Sometimes we want to get a collection of all the key values in the map. Sometimes, we need to extract the value of all values in the map.
very simply, it's easy to do with a two map-with-yourself approach:
Set Map.keyset (): Gets the collection of all keys in the map; Collection map.values (): Gets the collection of all the value in the map;
Show Me the Code:
Import java.util.Collection;
Import Java.util.Iterator;
Import Java.util.LinkedHashMap;
Import Java.util.Set;
public class Maptest {public
static void Main (string[] args) {
linkedhashmap<string, integer> Stringintegerlinkedhashmap = new linkedhashmap<string, integer> ();
Stringintegerlinkedhashmap.put ("A", 1);
Stringintegerlinkedhashmap.put ("B", 2);
Stringintegerlinkedhashmap.put ("C", 3);
Stringintegerlinkedhashmap.put ("D", 4);
Set<string> keyset = Stringintegerlinkedhashmap.keyset ();
collection<integer> Valueset = Stringintegerlinkedhashmap.values ();
iterator<string> keyiterator = Keyset.iterator ();
iterator<integer> valueiteator = Valueset.iterator ();
while (Keyiterator.hasnext ()) {
System.out.println (Keyiterator.next () + " " + valueiteator.next ());
}
}
}
The results of the operation are as follows:
A 1
B 2
C 3
D 4