List遍历删除

方法一:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("a");
list.add("ab");
list.add("abc");
list.add("ad");
Iterator<String> iterator = list.iterator();
while(iterator.hasNext()){
String next = iterator.next();
if("a".equals(next)||"c".equals(next)){
iterator.remove();
}
}
System.out.println(JSONObject.toJSONString(list));
}
输出:

1
["ab","abc","ad"]

方法二

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("a");
list.add("ab");
list.add("abc");
list.add("ad");
list.removeIf(s->"a".equals(s)||"c".equals(s));
System.out.println(JSONObject.toJSONString(list));
}
异常操作

List<String> list = Arrays.asList("a", "b", "abcd");
list.removeIf(s -> "a".equals(s) || "c".equals(s));
System.out.println(JSONObject.toJSONString(list));
Exception in thread "main" java.lang.UnsupportedOperationException
原因
@SafeVarargs
@SuppressWarnings("varargs")
public static <T > List < T > asList(T...a){
return new ArrayList<>(a);
}

/**
* @serial include
*/
private static class ArrayList<E> extends AbstractList<E>
implements RandomAccess, java.io.Serializable {

}

AbstractList做任何操作都是异常
public void add ( int index, E element){
throw new UnsupportedOperationException();
}

/**
* {@inheritDoc}
*
* <p>This implementation always throws an
* {@code UnsupportedOperationException}.
*
* @throws UnsupportedOperationException {@inheritDoc}
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E remove ( int index){
throw new UnsupportedOperationException();
}
修改
1
2
3
4
5
6
String [] str={"a","b","c","abcd"};
List<String> strs = Arrays.asList(str);
List<String> list = new ArrayList<>();
list.addAll(strs);
list.removeIf(s->"a".equals(s)||"c".equals(s));
System.out.println(JSONObject.toJSONString(list));
{% if post.top %} 置顶 | {% endif %}