This article describes four common ways and uses of Java to implement Map collection traversal. Share it for your reference, as follows:
~Map collections store values in the form of key-value pairs, so traversing Map collections is nothing more than getting keys and values, and obtaining keys and values according to actual needs.
1. It's nothing more than getting the value through map.keySet() and then getting the value according to the key
for(String s:map.keySet()){ System.out.println("key : "+s+" value : "+map.get(s));} 2. Get it through Map.Entry(String,String) , then use entry.getKey() to get the key, and get the value through entry.getValue()
for(Map.Entry<String, String> entry : map.entrySet()){ System.out.println("key key: "+entry.getKey()+" value: "+entry.getValue());}3. The purpose of using Iterator is to finally obtain entry, so understanding its usage can be used and mastered well.
package com.bie;import java.util.HashMap;import java.util.Iterator;import java.util.Map;/** * @author BieHongLi * @version Created at 8:58:54 pm on February 25, 2017 * */public class MapTest01 { public static void main(String[] args) { Map<String, String> map=new HashMap<String, String>(); map.put("Zhang San1", "Male"); map.put("Zhang San2", "Male"); map.put("Zhang San3", "Male"); map.put("Zhang San4", "Male"); map.put("Zhang San5", "Male"); //The first method of traversing maps is to strengthen the for loop map.keySet(), and then obtain the value value through the key key for(String s:map.keySet()){ System.out.println("key : "+s+" value : "+map.get(s)); } System.out.println("================================================================================================================================================================================================================================================================================================================================================================================================ s1:map.keySet()){//Transip the map's key System.out.println("key key: "+s1); } for(String s2:map.values()){//Transip the map's value System.out.println("value value: "+s2); } System.out.println("============================================================================================================================================================================================================================================================================================================================================================================================================================= System.out.println("key key: "+entry.getKey()+" value: "+entry.getValue()); } System.out.println("======================================================================================================================================== System.outer(); while(it.hasNext()){ Map.Entry<String, String>> it=map.entrySet().iterator(); while(it.hasNext()){ Map.Entry<String, String> entry=it.next(); System.out.println("key key:"+entry.getKey()+" value:"+entry.getValue()); } System.out.println("=======================================================================================================================================================================================================================================================================================================================================================================================================================================================================4. Some commonly used knowledge points of Map and the deformation forms of value need to be mastered and understood.
package com.bie;import java.util.Collection;import java.util.HashMap;import java.util.Map;import java.util.Set;/** * @author BieHongLi * @version Created: February 26, 2017 at 11:29:59 am * */public class MapTest02 { public static void main(String[] args) { //1:key and value are both of object types //2: key must be unique, not unique, then the value behind will overwrite the previous value //3: For HashMap, the key can be empty //4: value can be empty or empty //5: HashTable's key and value cannot be empty //6: properties' key and value must be String type Map<String , String> map=new HashMap<>(); map.put("null", "this is null 1"); map.put("null", "this is null 2"); System.out.println(map.size()); System.out.println(map.get(null)); System.out.println("===================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================== values=map.values();//Set of values System.out.println(values); System.out.println("============================================================================================================================================================================================================================================================================================================================================================================================================================================================================================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.