Java Traversal set Collection
1. Iterator Traversal:Set<string> set =NewHashset<string>(); Iterator<string> it =Set.iterator (); while(It.hasnext ()) {String str =It.next (); System.out.println (str); } 2. For loop traversal: for(String str:set) {System.out.println (str); } 3, the advantages are also reflected in generics if the set is stored in an objectSet<object> set =NewHashset<object>(); For loop traversal: for(Object obj:set) { if(objinstanceofInteger) { intAa=(Integer) obj; }Else if(objinstanceofString) { String AA =(String) obj } ........ }
Traverse Map in Java
map<string, string> map =NewHashMap (); Map.put ("Zhengzhou", "Henan"); Map.put ("Changsha", "Hunan"); //The firstset<string> set = Map.keyset ();//Take out all the key values for(String key:set) {System.out.println ("First type:" +Map.get (key)); } //The second KindSet<map.entry<string, string>> entryseset=Map.entryset (); for(Map.entry<string, string>Entry:entryseset) {System.out.println ("Second type:" +entry.getkey () + ":" +Entry.getvalue ()); } //Third KindIterator it =Map.keyset (). iterator (); while(It.hasnext ()) {//int key = (Integer) it.next ();String value =Map.get (It.next ()); System.out.println ("Third type:" +value); }
Traverse the map and set methods in Java to remove all keys from the map