7 traversal methods of HashMap

HashMap traversal method

In general terms, HashMap traversal methods can be divided into four categories:

  1. Iterator (Iterator) way to traverse;
  2. For Each way to traverse;
  3. Lambda expression traversal (after jdk1.8);
  4. Streams API traversal (after jdk1.8).

In these 4 categories, there are different traversal methods, so they can be subdivided into 7 categories:

  1. Use iterator (Iterator) EntrySet way to traverse;
  2. Use iterator (Iterator) KeySet to traverse;
  3. For Each EntrySet way to traverse;
  4. For Each KeySet way to traverse;
  5. Use Lambda expression to traverse;
  6. Use the Streams API to traverse in a single-threaded way;
  7. Use the Streams API to traverse in a multi-threaded manner.

Concrete code example

The following demonstrates with an example:

  1. Iterator (Iterator) EntrySet method
	public class One {
    
    
    //迭代器(Iterator)EntrySet方式
    public static void main(String[] args) {
    
    
        Map<Integer, String> map = new HashMap<>();
        map.put(1,"Java");
        map.put(2,"C++");
        map.put(3,"C");
        map.put(4,"Python");
        map.put(5,"Golang");

        //遍历
        Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
        while(iterator.hasNext()) {
    
    
            Map.Entry<Integer, String> entry = iterator.next();
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }
    }
}

Results of the:

1
Java
2
C++
3
C
4
Python
5
Golang

2. Iterator (Iterator) KeySet method

public class Two {
    
    
    //迭代器(Iterator)KeySet方式
    public static void main(String[] args) {
    
    
        Map<Integer, String> map = new HashMap<>();
        map.put(1,"Java");
        map.put(2,"C++");
        map.put(3,"C");
        map.put(4,"Python");
        map.put(5,"Golang");
        
        //遍历
        Iterator<Integer> iterator = map.keySet().iterator();
        while(iterator.hasNext()) {
    
    
            Integer key = iterator.next();
            System.out.println(key);
            System.out.println(map.get(key));
        }
    }
}

Results of the:

1
Java
2
C++
3
C
4
Python
5
Golang

3. For Each EntrySet method

public class Three {
    
    
    //ForEach EntrySet方式
    public static void main(String[] args) {
    
    
        Map<Integer, String> map = new HashMap<>();
        map.put(1,"Java");
        map.put(2,"C++");
        map.put(3,"C");
        map.put(4,"Python");
        map.put(5,"Golang");

        //遍历
        for (Map.Entry<Integer, String> entry : map.entrySet()){
    
    
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }
    }
}

Results of the:

1
Java
2
C++
3
C
4
Python
5
Golang

4. ForEach KeySet method

public class Four {
    
    
    //ForEach KeySet方式
    public static void main(String[] args) {
    
    
        Map<Integer, String> map = new HashMap<>();
        map.put(1,"Java");
        map.put(2,"C++");
        map.put(3,"C");
        map.put(4,"Python");
        map.put(5,"Golang");
        
        //遍历
        for (Integer key : map.keySet()){
    
    
            System.out.println(key);
            System.out.println(map.get(key));
        }
    }
}

Results of the:

1
Java
2
C++
3
C
4
Python
5
Golang

5. Lambda expression method

public class Five {
    
    
	//Lambda表达式方式
    public static void main(String[] args) {
    
    
        Map<Integer, String> map = new HashMap<>();
        map.put(1,"Java");
        map.put(2,"C++");
        map.put(3,"C");
        map.put(4,"Python");
        map.put(5,"Golang");

        //遍历
        map.forEach((key, value) -> {
    
    
            System.out.println(key);
            System.out.println(value);
        });
    }
}

Results of the:

1
Java
2
C++
3
C
4
Python
5
Golang

6. Streams API single-threaded mode

public class Six {
    
    
    //Streams API 单线程方式
    public static void main(String[] args) {
    
    
        Map<Integer, String> map = new HashMap<>();
        map.put(1,"Java");
        map.put(2,"C++");
        map.put(3,"C");
        map.put(4,"Python");
        map.put(5,"Golang");

        //遍历
        map.entrySet().stream().forEach((entry) -> {
    
    
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        });
    }
}

Results of the:

1
Java
2
C++
3
C
4
Python
5
Golang

7. Streams API multithreading method

public class Seven {
    
    
    //Streams API 多线程方式
    public static void main(String[] args) {
    
    
        Map<Integer, String> map = new HashMap<>();
        map.put(1,"Java");
        map.put(2,"C++");
        map.put(3,"C");
        map.put(4,"Python");
        map.put(5,"Golang");

        //遍历
        map.entrySet().parallelStream().forEach((entry) -> {
    
    
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        });
    }
}

Results of the:

1
Java
2
C++
3
C
4
Python
5
Golang

Summary: From the perspective of comprehensive performance and security, we should try to use the Iterator (Iterator) EntrySet method to traverse the Map collection.

Reference: https://mp.weixin.qq.com/s/zQBN3UvJDhRTKP6SzcZFKw

おすすめ

転載: blog.csdn.net/qq_44678607/article/details/130365883