Home > Java > Javagetting Started > Introduction to five methods of traversing a map

Introduction to five methods of traversing a map

王林
Release: 2020-10-14 15:45:42
forward
3644 people have browsed it

Introduction to five methods of traversing a map

Map collection traversal is often used in daily development. The differences between several traversal methods are introduced below.

(Recommended tutorial: java course)

1. How to write Iterator entrySet [recommended for JDK8 and below], Map.Entry is the internal interface of the Map interface, and obtains the iterator. Then take out the Map.Entry

        Iterator<Map.Entry<Integer,String>> iterator=map.entrySet().iterator();
        while(iterator.hasNext()){
            Map.Entry<Integer,String> entry=iterator1.next();
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }
Copy after login

2 in each iterator in turn. How to write Iterator keyset [not recommended, you can only get the key, and then get the corresponding value through the key, and repeat the calculation]

        Iterator<Integer> iterator=map.keySet().iterator();
        while (iterator.hasNext()){
            Integer key=iterator.next();
            System.out.println(key);
            System.out.println(map.get(key));
        }
Copy after login

3. Foreach traversal method [Recommended writing method below JDK8]

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

4. Lambda expression traversal [JDK8 recommended writing method, simple]

        map.forEach((key,value)->{
            System.out.println(key);
            System.out.println(value);
        });
Copy after login

5. Stream stream traversal Map [Writing method not recommended under JDK8] , Repeated calculation]

        map.entrySet().stream().forEach((Map.Entry<Integer, String> entry) -> {
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        });
Copy after login

If there is some intermediate processing in the Map collection, filtering operations can be performed, and streaming traversal is also very convenient.

Related recommendations: java introductory tutorial

The above is the detailed content of Introduction to five methods of traversing a map. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
map
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
Latest Issues
Leaflet gets all polylines added to map
From 1970-01-01 08:00:00
0
0
0
javascript - About Baidu Map API calling issues
From 1970-01-01 08:00:00
0
0
0
How to get the Baidu map api
From 1970-01-01 08:00:00
0
0
0
Looking for a javascript map marker plug-in
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template