search
HomeJavaJavagetting StartedWhat are the several ways of map traversal?

What are the several ways of map traversal?

Nov 19, 2020 pm 02:02 PM
mapTraverse

There are 4 ways to traverse map, namely: 1. Use for loop to traverse map; 2. Use iteration to traverse map; 3. Use keySet to iterate over map; 4. Use entrySet to traverse map.

What are the several ways of map traversal?

Several ways to traverse the map are as follows:

(Learning video sharing: java teaching video)

java code:

Map<String,String> map=new HashMap<String,String>();  
    map.put("username", "qq");  
    map.put("passWord", "123");  
    map.put("userID", "1");  
    map.put("email", "qq@qq.com");

Method one, for loop

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

Method two, iteration

Set set = map.entrySet();       
    Iterator i = set.iterator();       
    while(i.hasNext()){    
        Map.Entry<String, String> entry1=(Map.Entry<String, String>)i.next();  
        System.out.println(entry1.getKey()+"=="+entry1.getValue());  
    }

Method three, keySet() iteration

 Iterator it=map.keySet().iterator();  
    while(it.hasNext()){  
        String key;  
        String value;  
        key=it.next().toString();  
        value=map.get(key);  
        System.out.println(key+"--"+value);  
    }

Method 4, entrySet() iteration

Iterator it=map.entrySet().iterator();         
        System.out.println( map.entrySet().size());  
        String key;         
        String value;  
        while(it.hasNext()){  
               Map.Entry entry = (Map.Entry)it.next();         
               key=entry.getKey().toString();         
               value=entry.getValue().toString();         
               System.out.println(key+"===="+value);                   
        }      for (Map.Entry<String, String> entry : map.entrySet()) {
          System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
     }

Related recommendations: java introductory tutorial

The above is the detailed content of What are the several ways of map traversal?. For more information, please follow other related articles on 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function