This article analyzes several methods of java traversing maps. Share it for your reference, as follows:
Java code:
Map<String,String> map=new HashMap<String,String>();map.put("username", "qq");map.put("passWord", "123");map.put("userID", "1");map.put("email", "[email protected]");The first type uses a for loop
Java code:
for(Map.Entry<String, String> entry:map.entrySet()){ System.out.println(entry.getKey()+"--->"+entry.getValue());}The second type of iteration
Java code:
Set set = map.entrySet(); Iterator i = set.iterator(); while(i.hasNext()){ Map.Entry<String, String> entry1=(Map.Entry<String, String>)i.next(); System.out.println(entry1.getKey()+"=="+entry1.getValue());}Iterate with keySet()
Java code:
Iterator it=map.keySet().iterator(); while(it.hasNext()){ String key; String value; key=it.next().toString(); value=map.get(key); System.out.println(key+"--"+value);}Iterate with entrySet()
Java code:
Iterator it=map.entrySet().iterator();System.out.println( map.entrySet().size());String key;String value;while(it.hasNext()){ Map.Entry entry = (Map.Entry)it.next(); key=entry.getKey().toString(); value=entry.getValue().toString(); System.out.println(key+"===="+value);}For more Java-related content, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.