java List集合转Map并遍历输出

1.使用流转map并且遍历

package com.demo.toMap;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class MianDemo {
    public static void main(String[] args) {
        List<NodeList> list =  initList();
        //List通过流转Map<String,Integer>
        Map<String,Integer> map = list.stream().collect(Collectors.toMap(NodeList::getName, NodeList->NodeList.getAge()==null?1:NodeList.getAge()));

        //List通过流转Map<String,NodeList>
        Map<String,NodeList> mapObj = list.stream().collect(Collectors.toMap(NodeList::getName, NodeList->NodeList));

        System.out.println("=============================================");
        //map遍历方式1
        map.forEach((key,val)->{
            System.out.println("方式1 key: "+key+" , "+"val:"+val);
        });
        System.out.println("=============================================");
        //map遍历方式2
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            String key = entry.getKey();
            Integer value = entry.getValue();
            System.out.println("方式2 key: "+key+" , "+"val:"+value);
        }
        System.out.println("=============================================");
        //map遍历方式3,通过遍历 key 获取 val
        for (String key : map.keySet()) {
            // 根据键获取值
            Integer value = map.get(key);
            System.out.println("方式3 key: "+key+" , "+"val:"+value);
        }
        System.out.println("=============================================");
        //map遍历方式4,遍历values
        for (Integer val : map.values()) {
            System.out.println("方式4 val:"+val);
        }
        System.out.println("=============================================");
        //map遍历方式5,
        map.entrySet().forEach(entry->{
            System.out.println("方式5 key: "+entry.getKey()+" , "+"val:"+entry.getValue());
        });
        System.out.println("=============================================");
        //map遍历方式5,
        map.keySet().forEach(key->{
            Integer age =  map.get(key);
            System.out.println("方式5 key: "+key+" , "+"val:"+age);
        });

    }

    private static List<NodeList> initList(){
        List<NodeList> list = new ArrayList<>();
        NodeList node1 = new NodeList();
        node1.setName("a");
        node1.setAge(23);
        NodeList node2 = new NodeList();
        node2.setName("b");
        node2.setAge(23);
        NodeList node3 = new NodeList();
        node3.setName("c");
        node3.setAge(23);
        list.add(node1);
        list.add(node2);
        list.add(node3);
        return list;
    }
}

2.集合对象

package com.demo.toMap;

public class NodeList {

    private String name;

    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

 

posted @ 2024-07-15 17:12  晓海无涯苦揍舟  阅读(12)  评论(0编辑  收藏  举报