For a long time did not summarize some things, at the same time the collection part of the knowledge point of learning is relatively early, but never take time to study and study, today just have a moment to summarize the map commonly used traversal method:
Package Runningwhile;import Java.util.hashmap;import Java.util.iterator;import Java.util.map;public class hashmaptest {public static void main (string[] args) {map<string,string> TestMap = new Hashmap<string,string > ("Testmap.put", "4120000002"), Testmap.put ("name", "Lin Yu Ink"), Testmap.put ("Age", "" "), Testmap.put (" Class "," 04 "); Testmap.put ("Professional", "HVAC"); System.out.println ("-------------------The first Way---------------------");//The first method, the most primitive, the simplest, the lowest efficiency System.out.println ( Testmap.tostring ()); System.out.println (Testmap.keyset () +testmap.values (). toString ()); System.out.println ("-------------------The second Way---------------------"); for (String String:testmap.keySet ()) { System.out.println (string+ ":" +testmap.get (String));} System.out.println ("-------------------The Third Way---------------------");//output iterator<map.entry< via the Map iterator String, String>> iterator =testmap.entryset (). iterator (); while (Iterator.hasnext ()) {map.entry<string, string> infoentry = Iterator.next (); System.out.println (INFOENTRY.GETKEY () + ":" +infoentry.getvalue ());} System.out.println ("-------------------The Fourth Way---------------------");//Similar to the third method, but higher efficiency for (map.entry<string, String> Infoentry:testmap.entrySet ()) {System.out.println (Infoentry.getkey () + ":" +infoentry.getvalue ());}}}
The list also has its own iterator, with the following examples:
Iterator it = List.iterator ();
If you want to traverse the list, use Iterator.hasnext (), i.e.
while (It.hasnext ()) {
}
Java tool Classes Learn to organize--collection