The map itself does not have iterators and thus needs to take new steps while traversing the elements, providing two methods in the JDK
KeySet
KeySet ()
Returns a view of the
-
keys contained in this map
Set . The set is supported by mappings, so changes to the mappings can be reflected in this set, and vice versa. If the set is iterated while the mapping is modified (except through the iterator's own remove operation), the iteration result is indeterminate. Set support elements are removed via iterator.remove, set.remove, removeall, retainall , and C The Lear action removes the corresponding mapping from the map. It does not support add or addall operations.
-
-
Return:
The
-
set view of the keys contained in this map
1 Packagecom. Map;2 3 ImportJava.util.HashMap;4 ImportJava.util.HashSet;5 ImportJava.util.Iterator;6 ImportJava.util.Map;7 ImportJava.util.Random;8 ImportJava.util.Set;9 Ten Public classMapkeyset { One Public Static voidMain (string[] args) { Amap<string, string> map =NewHashmap<string, string>(); -Map.put ("The", "xsf")); -Map.put ("Up", "MDX"); theSet<string> KeySet =Map.keyset (); - //Get iterators -Iterator<string> it =keyset.iterator (); - while(It.hasnext ()) { +String key =It.next (); - //first remove key and get value by Get (key) +String value =Map.get (key); ASystem.out.println ("Key" +key+ "--->value:" +value); at - } - } -}
EntrySet
EntrySet ()
-
-
returns a view of the mapping relationships contained in this map
Set . The set is supported by mappings, so changes to the mappings can be reflected in this set, and vice versa. If the set is iterated while the mapping is modified (with the exception of the iterator's own remove operation, or by performing a setValue operation on the mapped item returned by the iterator), the iteration result is indeterminate. Set support elements are removed via iterator.remove, set.remove, removeall, retainall , and clear operation to remove the mapping relationship from the map. It does not support add or addall operations.
-
-
-
Return:
-
-
set view of mapping relationships contained in this map
-
1 Packagecom. Map;2 3 ImportJava.util.HashMap;4 ImportJava.util.Iterator;5 ImportJava.util.Map;6 ImportJava.util.Set;7 8 Public classMapentryset {9 Public Static voidMain (string[] args) {Tenmap<string,string> map =NewHashmap<string, string>(); OneMap.put ("The", "xsf")); AMap.put ("Up", "MDX"); - //Set EntrySet -set<map.entry<string, string>> entryset =Map.entryset (); the //put the Enterset into the iterator - /*iterator<map.entry<string, string>> it = Entryset.iterator (); - //Start Iteration - While (It.hasnext ()) { + //Get Map.entry Relationship Object - map.entry<string, string> me = It.next (); + String key = Me.getkey (); A String value = Me.getvalue (); at System.out.println ("Key:" +key+ "-->value:" +value); - }*/ - for(Map.entry<string, string>Entry:map.entrySet ()) { -String key =Entry.getkey (); -String value =Entry.getvalue (); -System.out.println ("Key:" +key+ "-->value:" +value); in } - to } +}
Map two kinds of traversal mode