Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the unique map traversal mode of JAVA8?

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about the unique way of traversing map in JAVA8, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Using the map traversal method brought about by JAV8 makes traversal very simple

Public class LambdaMap {private Map map = new HashMap (); @ Before public void initData () {map.put ("key1", "value1"); map.put ("key2", "value2"); map.put ("key3", "value3"); map.put ("key4", 4); map.put ("key5", 5); map.put ("key5",'h') } / * ways to traverse Map-* traversing key and value * / @ Test public void testErgodicWayOne () {System.out.println ("- Before JAVA8--") through Map.keySet For (String key: map.keySet ()) {System.out.println ("map.get (" + key + ") =" + map.get (key));} System.out.println ("- JAVA8 -") Map.keySet () .forEach (key-> System.out.println ("map.get (" + key + ") =" + map.get (key) } / * traversing Map the second * traversing key and value * / @ Test public void testErgodicWayTwo () {System.out.println ("- Before JAVA8--") through Map.entrySet using Iterator; Iterator iterator = map.entrySet () .iterator () While (iterator.hasNext ()) {Map.Entry entry = iterator.next (); System.out.println ("key:value =" + entry.getKey () + ":" + entry.getValue ());} System.out.println ("- JAVA8 -") Map.entrySet () .iterator () .forEachRemaining (item-> System.out.println ("key:value=" + item.getKey () + ":" + item.getValue () } / * traversing Map the third * traverses key and value through Map.entrySet. It is recommended to use * / @ Test public void testErgodicWayThree () {System.out.println ("- Before JAVA8--") for large capacity. For (Map.Entry entry: map.entrySet ()) {System.out.println ("key:value =" + entry.getKey () + ":" + entry.getValue ());} System.out.println ("- JAVA8 -") Map.entrySet () .forEach (entry-> System.out.println ("key:value =" + entry.getKey () + ":" + entry.getValue () } / * * traversing Map the fourth * traverses all value through Map.values (), but cannot traverse key * / @ Test public void testErgodicWayFour () {System.out.println ("- Before JAVA8--") For (Object value: map.values ()) {System.out.println ("map.value =" + value);} System.out.println ("- JAVA8 -"); map.values () .forEach (System.out::println) / / equivalent to map.values () .forEach (value-> System.out.println (value)) } / * * traversing the fifth kind of Map * through kmenv traversal, Java8's unique * / @ Test public void testErgodicWayFive () {System.out.println ("- Only JAVA8--") Map.forEach ((k, v)-> System.out.println ("key:value =" + k + ":" + v));}}

After reading the above, do you have any further understanding of JAVA8's unique way of traversing map? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report