public void removeList(List<String> list) {
for (int i = list.size()-1; i < list.size(); i--) {
if ("111".equals(list.get(i))) {
list.remove(i);
}
}
}
迭代器删除
public void removeList(List<String> list) {
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
if ("111".equals(iterator.next())) {
iterator.remove();//这里注意使用迭代器的删除,不能使用list的删除否则会报错ConcurrentModificationException
}
}
}
使用CopyOnWriteArrayList进行删除
public void removeList(List<String> list) {
final CopyOnWriteArrayList<String> copyOnWriteArrayList=new CopyOnWriteArrayList<>(list);
for (String s : copyOnWriteArrayList) {
if ("111".equals(s)) {
copyOnWriteArrayList.remove(s);
}
}
list=copyOnWriteArrayList;
}
map集合遍历删除
public void removeMap(Map<Integer, String> map) {
Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Integer, String> next = iterator.next();
Integer key = next.getKey();
iterator.remove();
}
//遍历map
for (Map.Entry<Integer, String> entry:map.entrySet()) {
System.out.println(entry.getValue());
}
}
反向删除public void removeList(List<String> list) { for (int i = list.size()-1; i < args.length; i++) { if ("111".equals(list.get(i))) { list.remove(i); ...