Java中遍历地图集合的方法有哪些

  介绍

这篇文章主要介绍了Java中遍历地图集合的方法有哪些,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获、下面让小编带着大家一起了解一下。

方式一通过地图。键盘使用迭代器遍历

@Test   public  void  testHashMap1 (), {   ,Map<整数,String>, map =, new  HashMap<在();   ,map.put(001年,“Java");   ,map.put(002,“数据库“);   ,map.put(003年,“Vue");   ,System.out.println(地图);      ,//通过Map.keySet使用迭代器遍历键,然后通过关键得到对应的价值值   ,Iterator iterator =, map.keySet () .iterator ();   ,while  (iterator.hasNext ()), {   ,Integer  key =, iterator.next ();   ,String  value =, map.get(关键);   ,System.out.println (“key =,,, +, key  +,,,, value =,,, +,值);   ,}   }

结果:

{1=Java, 2=数据库,3=Vue}
键=1,值=https://www.yisu.com/zixun/Java
键=2,value=https://www.yisu.com/zixun/数据库
=3,关键价值=https://www.yisu.com/zixun/Vue

方式二通过地图。entrySet使用迭代器遍历

@Test   public  void  testHashMap2 (), {   ,Map<整数,String>, map =, new  HashMap<在();   ,map.put(001年,“Java");   ,map.put(002,“数据库“);   ,map.put(003年,“Vue");   ,System.out.println(地图);      ,//通过Map.entrySet使用迭代器遍历键和值;注意,Set  entrySet():返回所有键值对构成的一套集合   ,Iterator祝辞,entries =, map.entrySet () .iterator ();   ,while  (entries.hasNext ()), {   ,Map.Entry<整数,String>, entry =, entries.next ();   ,System.out.println(入口);   ,}   }

结果:

{1=Java, 2=数据库,3=Vue}
Java
1=2=数据库
3=Vue

方式三通过地图。键盘遍历

@Test   public  void  testHashMap3 (), {   ,Map<整数,String>, map =, new  HashMap<在();   ,map.put(001年,“Java");   ,map.put(002,“数据库“);   ,map.put(003年,“Vue");   ,System.out.println(地图);      ,//通过Map.keySet遍历键,然后通过关键得到对应的价值值   ,for  (Integer  key : map.keySet ()), {   ,System.out.println (“key =,,, +, key  +,,,, value =,,, +, map.get(关键));   ,}   }

结果:

{1=Java, 2=数据库,3=Vue}
键=1,值=https://www.yisu.com/zixun/Java
键=2,value=https://www.yisu.com/zixun/数据库
=3,关键价值=https://www.yisu.com/zixun/Vue

方式四通过for - each迭代条目,使用地图。entrySet遍历

@Test   public  void  testHashMap4 (), {   ,Map<整数,String>, map =, new  HashMap<在();   ,map.put(001年,“Java");   ,map.put(002,“数据库“);   ,map.put(003年,“Vue");   ,System.out.println(地图);//大敌;使用for - each迭代条目,通过Map.entrySet遍历键和值   ,for  (Map.Entry<整数,,String>, entry :, map.entrySet ()), {   ,System.out.println (“key =,,, +, entry.getKey (), +,,,, value =,,, +, entry.getValue ());   ,}   }

{1=Java, 2=数据库,3=Vue}
键=1,值=https://www.yisu.com/zixun/Java
键=2,value=https://www.yisu.com/zixun/数据库
=3,关键价值=https://www.yisu.com/zixun/Vue

方式五使用λ表达式forEach遍历

@Test   public  void  testHashMap5 (), {   ,Map<整数,String>, map =, new  HashMap<在();   ,map.put(001年,“Java");   ,map.put(002,“数据库“);   ,map.put(003年,“Vue");   ,System.out.println(地图);//,使用λ表达式forEach遍历   ,map.forEach ((k, v),→, System.out.println (“key =,,, +, k  +,,,, value =,,, +, v));   }

<>强forEach源码

default  void  forEach (BiConsumer<?, super  K, ?, super  V>,动作),{   ,Objects.requireNonNull(行动);   ,for  (Map.Entry, entry :, entrySet ()), {   K 才能;k;   V 才能;v;   try {才能   时间=k 才能;entry.getKey ();   时间=v 才能;entry.getValue ();   },才能赶上(IllegalStateException 伊势),{//才能,却;能够usually  means 从而entry  is  no  longer 拷贝,地图。   throw 才能;new  ConcurrentModificationException (ise);   ,,}   action.accept才能(k, v);   null   null

Java中遍历地图集合的方法有哪些