赞
踩
HashMap<String , String> hashMap = new HashMap<>();
hashMap.put("1","name");
hashMap.put("2","age");
Iterator<Map.Entry<String, String>> iterator = hashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
Object key = entry.getKey();
Object val = entry.getValue();
System.out.println("key : " + key + "-----" + "val : " + val);
}
HashMap<String , String> hashMap = new HashMap<>();
hashMap.put("1","name");
hashMap.put("2","age");
Iterator<String> iterator = hashMap.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
Object val = hashMap.get(key);
System.out.println("key : " + key + "-----" + "val : " + val);
}
HashMap<String , String> hashMap = new HashMap<>();
hashMap.put("1","name");
hashMap.put("2","age");
for (Map.Entry<String, String> entry : hashMap.entrySet()) {
Object key = entry.getKey();
Object val = entry.getValue();
System.out.println("key : " + key + "-----" + "val : " + val);
}
HashMap<String , String> hashMap = new HashMap<>();
hashMap.put("1","name");
hashMap.put("2","age");
for (String key : hashMap.keySet()) {
Object val = hashMap.get(key);
System.out.println("key : " + key + "-----" + "val : " + val);
}
HashMap<String , String> hashMap = new HashMap<>();
hashMap.put("1","name");
hashMap.put("2","age");
hashMap.forEach((key , val) -> System.out.println("key : " + key + "-----" + "val : " + val));
HashMap<String , String> hashMap = new HashMap<>();
hashMap.put("1","name");
hashMap.put("2","age");
hashMap.entrySet().stream().forEach((entry) -> {
Object key = entry.getKey();
Object val = entry.getValue();
System.out.println("key : " + key + "-----" + "val : " + val);
});
HashMap<String , String> hashMap = new HashMap<>();
hashMap.put("1","name");
hashMap.put("2","age");
hashMap.entrySet().stream().parallel().forEach((entry) -> {
Object key = entry.getKey();
Object val = entry.getValue();
System.out.println("key : " + key + "-----" + "val : " + val);
});
我们不能在遍历Map时使用map.remove()方法 , 否则就会抛出异常 :
java.util.ConcurrentModificationException , 这种办法是非安全的 , 我们可以使用Iterator.remove() ,或者是Lambda 中的 removeIf() , 或者是Stream 中的 filter() 过滤或者删除相关数据
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。