Home > Java > javaTutorial > body text

Summary of several looping methods in Java Map

高洛峰
Release: 2017-01-22 16:28:41
Original
1719 people have browsed it

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 );               
}
Copy after login

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());    
}
Copy after login

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());    
 }
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!