Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement Map Collection traversal in Java

Shulou Source: shulou.com Published: 2022-06-02 18:47:14 10月16日 Update

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!

Tags: Way content more articles good practical capacity articles read knowledge reference help recommendations related Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MySQL Microsoft Linux Huawei