Java basics - Map collection traversal method

(1) Method 1: find value by key

  • First obtain the Set collection of all keys of the Map collection.
  • Traverse the Set collection of keys, and then extract the corresponding value through the key.

APIs involved:

method name illustrate
Set<K> keySet() Get the set of all keys
V get(Object key) get value by key

(2) Method 2: Key-value pairs

  • First convert the Map collection into a Set collection, and each element in the Set collection is a key-value pair implementation type.
  • Iterates through the Set collection, then extracts the keys and extracts the values.

APIs involved:

method name illustrate
Set<Map.Entry<K,V>> entrySet() Get a collection of all key-value pair objects
K getKey() get key
V getValue() get value

(3) Method 3: lambda expression

  • Thanks to the new technology Lambda expression started by JDK8, it provides a simpler and more direct way to traverse the collection.

APIs involved:

method name illustrate
default void forEach(BiConsumer<? super K, ? super V>action) Combining lambda to traverse the Map collection

process:

maps.forEach((k,v) -> {

        System.out.println(k + ":" + v);

});

(4) The codes of the three traversal methods are as follows:

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

/**
 * Map集合遍历方式
 */
public class MapDemo {
    public static void main(String[] args) {
        Map<String,Integer> maps = new HashMap<>();
        //无序,不重复,无索引
        maps.put("大大",23);
        maps.put("小小",21);
        maps.put("清清",20);
        maps.put("心心",24);
        System.out.println(maps);

        //1.键找值
        //1.1先拿到集合的全部键
        Set<String> keys = maps.keySet();
        //1.2遍历每个键,根据键提取值
        for (String key : keys) {
            int value = maps.get(key);
            System.out.println(key + ":" + value);
        }

        System.out.println(maps);//{大大=23, 小小=21, 心心=24, 清清=20}
        //2.键值对
        /**
         maps = {大大=23, 小小=21, 心心=24, 清清=20}
         使用foreach遍历Map集合,发现Map集合的键值对元素直接是没有类型的,所以不能使用foreach遍历集合
         可以通过调用Map的方法,entrySet把Map集合转换成Set集合形式 maps.entrySet()
         Set<Map.Entry<String,Integer>>entries = [(大大=23), (小小=21), (心心=24), (清清=20)]
         此时就可以使用foreach遍历
         */
        //2.1把Map集合转换成Set集合 快捷键:maps.entrySet() + ctrl+alt+v
        Set<Map.Entry<String, Integer>> entries = maps.entrySet();
        //2.2开始遍历
        for(Map.Entry<String, Integer> entry : entries){
            String key = entry.getKey();
            int value = entry.getValue();
            System.out.println(key + ":" + value);
        }

        System.out.println(maps);//{大大=23, 小小=21, 心心=24, 清清=20}
        //3.Lambda表达式
//        maps.forEach(new BiConsumer<String, Integer>() {
//            @Override
//            public void accept(String key, Integer value) {
//                System.out.println(key + ":" + value);
//            }
//        });
        
        //简化代码
        maps.forEach ((k, v) -> {System.out.println(k + ":" + v);});
    }
}

Guess you like

Origin blog.csdn.net/weixin_61275790/article/details/130058807