In general, the most common implementation class for a map is HashMap.
The traversal of a map is also an iterative
1. Use entries to traverse in the For-each loop (both key and value are taken)
New Hashmap<string, string>(); for (map.entry<string, string>map.entryset ()) { System.out.println(entry.getkey () + ", Value:" + entry.getvalue ()); }
2. Traverse keys or values (value or Fetch key) in the For-each loop
New Hashmap<string, string>(); // Fetch key for Map.keyset ()) { System.out.println ("key:" + key); } //Fetch value for map.values ()) { System.out.println ("value:" + value); }
3. Using iterator traversal
New Hashmap<string, string>(); Iterator<map.entry<string, string>> entries = map.entryset (). Iterator (); while (Entries.hasnext ()) { map.entry<string, string> Entry = entries.next (); System.out.println ("Key:" + entry.getkey () + ", Value:" + Entry.getvalue ()); }
4. Key to find the value traversal get (key), it is generally known that a key directly get the value, no longer loop traversal.
Traversal of the Java map object