Home >Java >javaTutorial >Summary of several looping methods in Java Map

Summary of several looping methods in Java Map

高洛峰
高洛峰Original
2017-01-22 16:28:411802browse

According to the new features of JDK, use For to loop Map, such as looping the Key of Map

Java code

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

What should be noted here is how paraMap is defined. If it is a simple Map paraMap = new HashMap(); then The previous String can only be replaced with Object.

Loop through the key and value of the entire Map, as follows:

Java code

for(Map.Entry<String, Object> entry : paraMap.entrySet())    
{    
    System.out.println(entry.getKey()+": "+entry.getValue());    
}

In the past, it was looped 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());    
 }

For more related articles summarizing several looping methods of Java Map, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn