HashMap的四种循环遍历方式
2018-04-09 23:32
316 查看
public static void main(String[] args){ HashMap<Integer,Integer> hashMap = new HashMap<Integer, Integer>(); hashMap.put(1, 1); hashMap.put(2, 2); //* 4种hashMap循环方式 // for each map.entrySet() for(Entry<Integer, Integer> entry : hashMap.entrySet()){ System.out.println(entry.getKey()); System.out.println(entry.getValue()); } //for each map.entrySet(),用临时变量保存map.entrySet() Set<Entry<Integer, Integer>> maps = hashMap.entrySet(); for(Entry<Integer, Integer> entry : maps){ System.out.println(entry.getKey()); System.out.println(entry.getValue()); } //显示调用map.entrySet()的集合迭代器 Iterator<Entry<Integer, Integer>> iterator2 = hashMap.entrySet().iterator(); while(iterator2.hasNext()){ Entry<Integer, Integer> entry = iterator2.next(); System.out.println(entry.getKey()); System.out.println(entry.getValue()); } // for each map.keySet(),再调用get获取 for(Integer key : hashMap.keySet()){ System.out.println(key); System.out.println(hashMap.get(key)); } }
相关文章推荐
- Java --- HashMap的四种循环遍历方式及性能对比
- Java中HashMap遍历的四种方式
- HashMap三种循环遍历方式及其性能对比
- HashMap循环遍历方式及其性能对比
- Java中HashMap遍历的四种方式
- HashMap循环遍历方式及其性能对比
- HashMap最优的循环遍历方式
- HashMap循环遍历方式及其性能对比
- Java——HashMap<> 四种遍历方式
- HashMap循环遍历方式及其性能对比
- HashMap循环遍历方式及其性能对比
- 遍历HashMap的四种方式
- HashMap循环遍历方式
- HashMap循环遍历方式及其性能对比
- HashMap循环遍历方式及其性能对比
- Java中HashMap遍历的四种方式
- HashMap循环遍历方式及其性能对比
- HashMap循环遍历方式及其性能对比
- Java中HashMap遍历的四种方式
- HashMap 4种循环遍历方式及其性能对比