Java is parsed in detail with a for loop Map

  • 2020-04-01 02:35:47
  • OfStack

According to JDK5's new feature, a For loop Map, such as a loop Map Key, is used


for(String dataKey : paraMap.keySet())   {    
    System.out.println(dataKey );               
}

Notice how paraMap is defined, if it's a simple Map paraMap = new HashMap (); Then the String in front of it can only be changed to Object.

Loop the key and value of the entire map


Map<Integer,String> map = new LinkedHashMap<Integer,String>(); 
map.put(1, " Monday "); 
map.put(2, " Tuesday "); 
map.put(3, " Wednesday "); 
map.put(4, " Thursday "); 
map.put(5, " Friday "); 
map.put(6, " Saturday "); 
map.put(7, " Sunday "); 

for(Map.Entry<Integer, String> entry: map.entrySet()) { 
 System.out.print(entry.getKey() + ":" + entry.getValue() + "t"); 
}

Output:

1: Monday 2: Tuesday 3: Wednesday 4: Thursday 5: Friday 6: Saturday 7: Sunday


Related articles: