Home Java Javagetting Started What are the several ways of map traversal?

What are the several ways of map traversal?

Nov 19, 2020 pm 02:02 PM
map Traverse

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

Method one, for loop

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

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

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

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

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 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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does springboot read lists, arrays, map collections and objects in yml files? How does springboot read lists, arrays, map collections and objects in yml files? May 11, 2023 am 10:46 AM

application.yml defines the list collection. The first way is to use the @ConfigurationProperties annotation to obtain all the values ​​​​of the list collection type:code:status:-200-300-400-500. Write the entity class corresponding to the configuration file. What needs to be noted here is that defining the list Collection, first define a configuration class Bean, and then use the annotation @ConfigurationProperties annotation to obtain the list collection value. Here we will explain the role of the relevant annotations. @Component hands over the entity class to Spring management @ConfigurationPropertie

How to set expiration time map in Java How to set expiration time map in Java May 04, 2023 am 10:13 AM

1. Technical background In actual project development, we often use caching middleware (such as redis, MemCache, etc.) to help us improve the availability and robustness of the system. But in many cases, if the project is relatively simple, there is no need to specifically introduce middleware such as Redis to increase the complexity of the system in order to use caching. So does Java itself have any useful lightweight caching components? The answer is of course yes, and there is more than one way. Common solutions include: ExpiringMap, LoadingCache and HashMap-based packaging. 2. Technical effects to realize common functions of cache, such as outdated deletion strategy, hotspot data warm-up 3. ExpiringMap3.

Java how to loop through a folder and get all file names Java how to loop through a folder and get all file names Mar 29, 2024 pm 01:24 PM

Java is a popular programming language with powerful file handling capabilities. In Java, traversing a folder and getting all file names is a common operation, which can help us quickly locate and process files in a specific directory. This article will introduce how to implement a method of traversing a folder and getting all file names in Java, and provide specific code examples. 1. Use the recursive method to traverse the folder. We can use the recursive method to traverse the folder. The recursive method is a way of calling itself, which can effectively traverse the folder.

What are the ways to implement thread safety for Map in Java? What are the ways to implement thread safety for Map in Java? Apr 19, 2023 pm 07:52 PM

Method 1. Use HashtableMapashtable=newHashtable(); This is the first thing everyone thinks of, so why is it thread-safe? Then take a look at its source code. We can see that our commonly used methods such as put, get, and containsKey are all synchronous, so it is thread-safe publicsynchronizedbooleancontainsKey(Objectkey){Entrytab[]=table;inthash=key.hashCode( );intindex=(hash&0x7FFFFFFF)%tab.leng

How to convert objects to Maps in Java - using BeanMap How to convert objects to Maps in Java - using BeanMap May 08, 2023 pm 03:49 PM

There are many ways to convert javabeans and maps, such as: 1. Convert beans to json through ObjectMapper, and then convert json to map. However, this method is complicated and inefficient. After testing, 10,000 beans were converted in a loop. , it takes 12 seconds! ! ! Not recommended. 2. Obtain the attributes and values ​​of the bean class through Java reflection, and then convert them into the key-value pairs corresponding to the map. This method is the second best, but it is a little more troublesome. 3. Through net.sf.cglib.beans.BeanMap Method in the class, this method is extremely efficient. The difference between it and the second method is that because of the use of cache, the bean needs to be initialized when it is first created.

Optimize the performance of Go language map Optimize the performance of Go language map Mar 23, 2024 pm 12:06 PM

Optimizing the performance of Go language map In Go language, map is a very commonly used data structure, used to store a collection of key-value pairs. However, map performance may suffer when processing large amounts of data. In order to improve the performance of map, we can take some optimization measures to reduce the time complexity of map operations, thereby improving the execution efficiency of the program. 1. Pre-allocate map capacity. When creating a map, we can reduce the number of map expansions and improve program performance by pre-allocating capacity. Generally, we

How to configure and use the map module in Nginx server How to configure and use the map module in Nginx server May 21, 2023 pm 05:14 PM

The map directive uses the ngx_http_map_module module. By default, nginx loads this module unless artificially --without-http_map_module. The ngx_http_map_module module can create variables whose values ​​are associated with the values ​​of other variables. Allows classification or simultaneous mapping of multiple values ​​​​to multiple different values ​​​​and storage in a variable. The map directive is used to create a variable, but only performs the view mapping operation when the variable is accepted. For processing requests that do not reference variables, this The module has no performance shortcomings. 1.ngx_http_map_module module instruction description map syntax

How to get map elements in go language How to get map elements in go language Jan 16, 2023 am 10:38 AM

Two methods: 1. Use the "for range" statement to traverse the map to obtain all elements, with the syntax "for key, value := range mapName{...}". 2. Use key as an index to obtain the specified element, the syntax is "value, isOk := mapName[key]"; return two return values, the first return value is the obtained value, if the key does not exist, return empty Value, the second parameter is a bool value, indicating whether the value is obtained successfully.

See all articles