正确删除
边遍历边修改 Collection 的唯一正确方式是使用 Iterator.remove() 方法,如下:
Iterator<Integer> it = list.iterator();
while(it.hasNext()){
*// do something*
it.remove();
}
如果是List
List<Integer> list3 = new ArrayList<>();
list3.add(1);
list3.add(2);
list3.add(3);
list3.add(4);
Iterator<Integer> iterator = list3.iterator();
while (iterator.hasNext()){
if(iterator.next() % 2 == 0){
iterator.remove();
}
}
System.out.println(list3);
结果为[1, 3]
set同上
如果是Map
for (Iterator<Map.entry<K, V>> it = myHashMap.entrySet().iterator; it.hasNext();){
Map.Entry<K, V> item = it.next();
K key = item.getKey();
V val = item.getValue();
//todo with key and val
//you may remove this item using "it.remove();"
}
或者
HashMap<Integer, String> hmap = new HashMap<>();
hmap.put(17, "h_nancy");
hmap.put(18, "h_jerry");
hmap.put(10, "h_lucy");
hmap.put(12, "h_henrly");
Iterator<Map.Entry<Integer, String>> it = hmap.entrySet().iterator();
System.out.println("删除元素前:" + hmap);
while (it.hasNext()){
Map.Entry<Integer, String> next = it.next();
Integer key = next.getKey();
String v = next.getValue();
if("h_lucy".equals(v)){
it.remove();
}
}
System.out.println("删除h_lucy后:" + hmap);
删除元素前:{17=h_nancy, 18=h_jerry, 10=h_lucy, 12=h_henrly}
删除lucy后:{17=h_nancy, 18=h_jerry, 12=h_henrly}
一种最常见的错误代码
for(Integer i : list){
list.remove(i)
}
运行以上错误代码会报 ConcurrentModificationException 异常。这是因为当使用 foreach(for(Integer i : list)) 语句时,会自动生成一个iterator 来遍历该 list,但同时该 list 正在被 Iterator.remove() 修改。Java 一般不允许一个线程在遍历 Collection 时另一个线程修改它。
使用foreach遍历List,但不能对某一个元素进行操作(这种方法在遍历数组和Map集合的时候同样适用)
import java.util.ArrayList;
public class Demo02 {
public static void main(String[] args) {
ArrayList<News> list = new ArrayList<News>();
list.add(new News(1,"list1","a"));
list.add(new News(2,"list2","b"));
list.add(new News(3,"list3","c"));
list.add(new News(4,"list4","d"));
for (News s : list) {
System.out.println(s.getId()+" "+s.getTitle()+" "+s.getAuthor());
}
}
}
各种遍历
List集合
List<String> list = new ArrayList<>();
list.add("henrly");
list.add("nancy");
list.add("lucy");
list.add("jeacy");
//遍历List集合
//1.使用for循环
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
//2.使用迭代器
Iterator<String> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
//3.使用增强for循环
for (String l : list) {
System.out.println(l);
}
Set集合
Set<Integer> set = new TreeSet<>();
set.add(111);
set.add(222);
set.add(333);
set.add(444);
//遍历Set集合
//Set集合无索引,所以无法使用for循环遍历
//1.使用迭代器
Iterator<Integer> it = set.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
//2.使用增强for循环
for (Integer i : set) {
System.out.println(i);
}
Map集合
HashMap<Integer, String> hmap = new HashMap<>();
hmap.put(17, "h_nancy");
hmap.put(18, "h_jerry");
hmap.put(10, "h_lucy");
hmap.put(12, "h_henrly");
TreeMap<Integer, String> map = new TreeMap<>();
map.put(17, "nancy");
map.put(18, "jerry");
map.put(10, "lucy");
map.put(12, "henrly");
//遍历HashMap集合
//Map集合的遍历使用keySet和entrySet
//1.使用keySet
//Set<Integer> integers = hmap.keySet();
for (Integer key : hmap.keySet()) {
System.out.println(key + "----" + hmap.get(key));
}
System.out.println("------------------------------------------------");
//2.使用entrySet
//Set<Map.Entry<Integer, String>> entries = hmap.entrySet();
for (Map.Entry<Integer, String> entry : hmap.entrySet()) {
System.out.println(entry);
}
//遍历HashMap集合
//Map集合的遍历使用keySet和entrySet
//1.使用keySet
for (Integer key : map.keySet()) {
System.out.println(key + "----" + hmap.get(key));
}
System.out.println("------------------------------------------------");
//2.使用entrySet
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + "---" + entry.getValue());
}