Summary of several looping ways of Java Map
- 2020-04-01 02:35:44
- OfStack
According to the JDK's new features, a For loop Map, such as a loop Map Key, is used
Java code
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.
The key and value of the entire Map are looped as follows:
Java code
for(Map.Entry<String, Object> entry : paraMap.entrySet())
{
System.out.println(entry.getKey()+": "+entry.getValue());
}
In the old days, it would have been like this:
Java code
Iterator it = paraMap.entrySet().iterator();
while (it.hasNext())
{
Map.Entry pairs = (Map.Entry)it.next();
System.out.println(pairs.getKey() + " = " + pairs.getValue());
}