怎么在Java8中利用forEach语句对列表和地图进行循环

  介绍

本文章向大家介绍怎么在Java8中利用forEach语句对列表和地图进行循环的基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Java是什么

Java是一门面向对象编程语言,可以编写桌面应用程序,网络应用程序,分布式系统和嵌入式系统应用程序。

1, forEach和地图

<强> 1.1,常规循环地图常用的方法。

Map, items =, new  HashMap<在(),,      items.put (“A" 10),,   items.put (“B" 20),,   items.put (“C" 30),,   items.put (“D" 40),,   items.put (“E", 50),,   items.put (“F" 60),,      (Map.Entry, entry :, items.entrySet ()) {,   ,System.out.println (“Item :,“+ enty.getKey (), +,“, Count :“, +, entry.getVlaue ()),   }

<强> 1.2,在Java 8中,你可以用forEach +λ表达式来循环地图

Map<字符串,Integer>, items =, new  HashMap<在(),,   items.put (“A",, 10),,      items.put (“B",, 20),,   items.put (“C",, 30),,   items.put (40)“D",,,,      items.put (50)“E",,,,   items.put (60)“F",,,,   items.forEach ((k、v)→System.out.println (“, Item :,“+, k  +,“, Count ,:,“, +, v)),,//items.forEach ((k、v)→{System.out.println (“, Item :,“+, k  +,“, Count ,:,“, +, v);}),,   items.forEach ((k、v)→{,   ,System.out.println (“Item :,“, +, k  +,“, Count ,:,“, +, v),,   ,如果(“E" .equals (k)) {,   ,System.out.println (“Hello  E");,   }大敌;   });

2, forEach和列表

<强> 2.1,常规的循环列表的方法

, List, items =, new  ArrayList<在(),,   ,items.add (“Anna");,   ,items.add (“Brian");,   ,items.add (“Cici");,   ,items.add (“Elena");,   ,//常规的循环列表的方法,   ,(String  item :项目){,   System.out.println才能(项),,   以前,}

2.2,在Java 8中,你可以用forEach +λ表达式,或者方法调用来循环地图

Map<字符串,Integer>, items =, new  HashMap<在(),,   items.put (“A",, 10),,   items.put (“B",, 20),,   items.put (“C",, 30),,   items.put (40)“D",,,,   items.put (50)“E",,,,   items.put (60)“F",,,,//在Java  8中,,你可以用forEach  +,λ表达式来循环Map //输出上面放的所有列表,   items.forEach ((k、v)→System.out.println (“, Item :,“+, k  +,“, Count ,:,“, +, v)),,//items.forEach ((k、v)→{System.out.println (“, Item :,“+, k  +,“, Count ,:,“, +, v);}),,//输出上面放的所有列表,+,Hello  E    items.forEach ((k、v)→{,   ,System.out.println (“Item :,“, +, k  +,“, Count ,:,“, +, v),,   ,如果(“E" .equals (k)) {,   ,System.out.println (“Hello  E");,   }大敌;   }),,   地图   package  com.foreach.learn,   import  java.util.HashMap,   import  java.util.Map,   {public  class  forEachLearnMap    ,public  static  void  main (String [], args), {,   ,//forEach 和,Map    ,Map<字符串,Integer>, items =, new  HashMap<在(),,   ,items.put (“A",, 10),,   ,items.put (“B",, 20),,   ,items.put (“C",, 30),,   ,items.put (40)“D",,,,   ,items.put (50)“E",,,,   ,items.put (60)“F",,,,   ,//常规的循环地图的方法,   ,(字符串,Map.Entry, entry :, items.entrySet ()), {,   System.out.println才能(“,Item :,“+, entry.getKey (), +,“, Count ,:,“, +, entry.getValue ());,,   }大敌;   ,//在Java  8中,,你可以用forEach  +,λ表达式来循环Map    ,//输出上面放的所有列表,   ,items.forEach ((k、v)→System.out.println (“, Item :,“+, k  +,“, Count ,:,“, +, v)),,   ,//items.forEach ((k、v)→{System.out.println (“, Item :,“+, k  +,“, Count ,:,“, +, v);}),,   ,//输出上面放的所有列表,+,Hello  E    ,items.forEach ((k、v)→{,   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

怎么在Java8中利用forEach语句对列表和地图进行循环