hashMap循环遍历与删除
http://www.cnblogs.com/zhangnf/p/HashMap.html?utm_source=itdadao&utm_medium=referral
HashMap的遍历主要有两种方式:
第一种采用的是foreach模式,适用于不需要修改HashMap内元素的遍历,只需要获取元素的键/值的情况。
HashMap<K, V> myHashMap;
for (Map.entry<K, V> item : myHashMap.entrySet()){
K key = item.getKey();
V val = item.getValue();
//todo with key and val
//WARNING: DO NOT CHANGE key AND val IF YOU WANT TO REMOVE ITEMS LATER
}
第二种采用迭代器遍历,不仅适用于HashMap,对其它类型的容器同样适用。
采用这种方法的遍历,可以用下文提及的方式安全地对HashMap内的元素进行修改,并不会对后续的删除操作造成影响。
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之删除元素
如果采用第一种的遍历方法删除HashMap中的元素,Java很有可能会在运行时抛出异常。
HashMap<String, Integer> myHashMap = new HashMap<>();
myHashMap.put("1", 1);
myHashMap.put("2", 2);
for (Map.Entry<String, Integer> item : myHashMap.entrySet()){
myHashMap.remove(item.getKey());
}
for (Map.Entry<String, Integer> item : myHashMap.entrySet()){
System.out.println(item.getKey());
}
运行上面的代码,Java抛出了 java.util.ConcurrentModificationException 的异常。并附有如下信息。
at java.util.HashMap
HashIterator.nextNode(UnknownSource)atjava.util.HashMap
EntryIterator.next(Unknown Source)
at java.util.HashMap$EntryIterator.next(Unknown Source)
可以推测,由于我们在遍历HashMap的元素过程中删除了当前所在元素,下一个待访问的元素的指针也由此丢失了。
所以,我们改用第二种遍历方式。
代码如下:
for (Iterator<Map.Entry<String, Integer>> it = myHashMap.entrySet().iterator(); it.hasNext();){
Map.Entry<String, Integer> item = it.next();
//... todo with item
it.remove();
}
for (Map.Entry<String, Integer> item : myHashMap.entrySet()){
System.out.println(item.getKey());
}
运行结果没有显示,表明HashMap中的元素被正确删除了
遍历hashMap的例子
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("1", "value1");
map.put("2", "value2");
map.put("3", "value3");
//第一种:普遍使用,二次取值
System.out.println("通过Map.keySet遍历key和value:");
for (String key : map.keySet()) {
System.out.println("key= "+ key + " and value= " + map.get(key));
}
//第二种
System.out.println("通过Map.entrySet使用iterator遍历key和value:");
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
//第三种:推荐,尤其是容量大时
System.out.println("通过Map.entrySet遍历key和value");
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
//第四种
System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
for (String v : map.values()) {
System.out.println("value= " + v);
}
}
//第五种
Map<String, String> testMap = new HashMap<String, String>();
testMap.put("1", "test1");
testMap.put("2", "test2");
testMap.put("3", "test3");
System.out.println(testMap);
// 获取Map的values
Collection<String> testCollection = testMap.values();
System.out.println(testCollection);
for (String temp : testCollection) {
System.out.println("temp:" + temp);
}
复杂的遍历HashMap的例子
Map<String, HashMap<String, String>> testMap2 = new HashMap<String, HashMap<String, String>>();
HashMap<String, String> test4Map = new HashMap<String, String>();
test4Map.put("41", "lvyuan41");
test4Map.put("42", "lvyuan42");
testMap2.put("key1", test4Map);
HashMap<String, String> test5Map = new HashMap<String, String>();
test5Map.put("51", "lvyuan51");
test5Map.put("52", "lvyuan52");
testMap2.put("key2", test5Map);
System.out.println(testMap2);
//使用循环方式取出haspMap中的值
Collection<HashMap<String, String>> testCollection = testMap2.values();
System.out.println(testCollection);
for(HashMap<String,String> temp:testCollection){
for(Map.Entry<String, String> entry:temp.entrySet()){
System.out.println(entry.getValue());
}
}
//使用迭代器方式取出hashMap中的值
Iterator<HashMap<String, String>> it = testCollection.iterator();
while(it.hasNext()){
HashMap<String, String> tempMap = it.next();
Iterator<Map.Entry<String,String>> it2 = tempMap.entrySet().iterator();
while(it2.hasNext()){
Map.Entry<String,String> temp = it2.next();
if(temp.getKey().equals("51")){
it2.remove();
}
}
}
System.out.println("----------");
System.out.println(testCollection);
System.out.println("----------");