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

How to implement Map Collection traversal in Java

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about how to implement traversal of Map collections in Java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Map collection traversal mode

Method 1: through map.keySet (), as follows:

Public void mapKeySet (Map map) {

/ / get the collection of the map collection key through map.keySet () and then traverse the key to get the value according to the key

For (String key:map.keySet ()) {

String value = map.get (key) .toString (); / / get the value through key

System.out.println ("key:" + key+ "vlaue:" + value)

}

} method 2: use iterator through Map.entrySet, as follows:

Public void mapEntrySet (Map map) {

/ / Map.entrySet traverses using iterator to get key and value values

Iterator entry = map.entrySet () .iterator ()

While (entry.hasNext ()) {

Entry newEntry = entry.next ()

System.out.println ("key:" + newEntry.getKey () + "value:" + newEntry.getValue ())

}

} method 3: through Map.entrySet (), as follows:

Public void mapEntrySet (Map map) {

/ / through map. EntrySet () to traverse key and value

For (Map.Entry entry: map.entrySet ()) {

System.out.println ("key:" + entry.getKey () + "value:" + entry.getValue ());}

} Note: this method is recommended, especially for Map collections with large capacity

Method 4: get all the value by Map.values () traversal, as follows:

Public void mapValues (Map map) {

For (Object obj:map.values ()) {

System.out.println (obj)

}

} Note: the map collection key cannot be obtained in this way

Thank you for reading! On "how to achieve Map collection traversal in Java" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

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