HashMap3种遍历方式
1. 通过键值对遍历:先将hashMap实例转化为set实例(类型为map.entry<>),
Iterator<Map.Entry<Integer, Integer>> iterator = hashMap.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry<Integer, Integer> next = iterator.next();
Integer key = next.getKey();
Integer value = next.getValue();
System.out.print(key+":"+value+".");
}
2. 通过键来遍历 (hashMap.keySet().iterator();的底层操作流程)
Iterator<Integer> iterator1 = hashMap.keySet().iterator();
while(iterator1.hasNext()){
Integer next = iterator1.next();
System.out.print(next+".");
System.out.print(hashMap.get(next)+",");
}
3. 通过值来遍历 ( hashMap.values().iterator())
Iterator<Integer> iterator2 = hashMap.values().iterator();
while (iterator2.hasNext()) {
Integer next = iterator2.next();
System.out.print(next + " ");
}