展会信息港展会大全

Java遍历Map时 删除元素
来源:互联网   发布日期:2016-01-26 10:29:06   浏览:1701次  

导读:public class HashMapTest { private static MapInteger, String map=new HashMapInteger,String(); ** 1 HashMap 类映射不保证顺序;某些映射可明确保证其顺序: TreeMap 类 * 2 在遍 ...

public class HashMapTest {

private static Map<Integer, String> map=new HashMap<Integer,String>();

/**1.HashMap 类映射不保证顺序;某些映射可明确保证其顺序: TreeMap 类

*2.在遍历Map过程中,不能用map.put(key,newVal),map.remove(key)来修改和删除元素,

*会引发 并发修改异常,可以通过迭代器的remove():

*从迭代器指向的 collection 中移除当前迭代元素

*来达到删除访问中的元素的目的。

**/

public static void main(String[] args) {

map.put(1,"one");

map.put(2,"two");

map.put(3,"three");

map.put(4,"four");

map.put(5,"five");

map.put(6,"six");

map.put(7,"seven");

map.put(8,"eight");

map.put(5,"five");

map.put(9,"nine");

map.put(10,"ten");

Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();

while(it.hasNext()){

Map.Entry<Integer, String> entry=it.next();

int key=entry.getKey();

if(key%2==1){

System.out.println("delete this: "+key+" = "+key);

//map.put(key, "奇数");//ConcurrentModificationException

//map.remove(key);//ConcurrentModificationException

it.remove();//OK

}

}

//遍历当前的map;这种新的for循环无法修改map内容,因为不通过迭代器。

System.out.println("-------nt最终的map的元素遍历:");

for(Map.Entry<Integer, String> entry:map.entrySet()){

int k=entry.getKey();

String v=entry.getValue();

System.out.println(k+" = "+v);

}

}

}

赞助本站

人工智能实验室

相关热词: 遍历Map 删除元素 Java

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港