Stream流List转成Map的几种方式

实体:

public class Person {
    private String name;
    private String skill;
    public Person(String name, String skill) {
        this.name = name;
        this.skill= skill;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSkill() {
        return skill;
    }
    public void setSkill(String skill) {
        this.skill = skill;
    }
}

一、List<Object> 转成Map<String,Object>

List<Person> list = new ArrayList<>();
Person person1 = new Person("孙悟空","筋斗云");
Person person2 = new Person("猪八戒","大师兄,师傅被妖怪抓走了");
Person person3 = new Person("沙僧","大师兄,师傅跟二师兄被妖怪抓走了");
Person person4 = new Person("孙悟空","72变");
list.add(person1);
list.add(person2);
list.add(person3);
list.add(person4);
Map<String,Person> map =  list.stream().collect(Collectors.toMap(Person::getName, each->each,(value1, value2) -> value1));
System.out.println(JSON.toJSONString(map));
图片[1]-Stream流List转成Map的几种方式-深吸氧

二、List<Object> 转成Map<String,String>

List<Person> list = new ArrayList<>();
Person person1 = new Person("孙悟空","筋斗云");
Person person2 = new Person("猪八戒","大师兄,师傅被妖怪抓走了");
Person person3 = new Person("沙僧","大师兄,师傅跟二师兄被妖怪抓走了");
Person person4 = new Person("孙悟空","72变");
list.add(person1);
list.add(person2);
list.add(person3);
list.add(person4);
Map<String,String> map =  list.stream().collect(Collectors.toMap(Person::getName,Person::getSkill,(value1, value2) -> value1));
System.out.println(JSON.toJSONString(map));
图片[2]-Stream流List转成Map的几种方式-深吸氧

三、List<Object> 转成Map<String,List<Object>>

1. 方式一

List<Person> list = new ArrayList<>();
Person person1 = new Person("孙悟空","筋斗云");
Person person2 = new Person("猪八戒","大师兄,师傅被妖怪抓走了");
Person person3 = new Person("沙僧","大师兄,师傅跟二师兄被妖怪抓走了");
Person person4 = new Person("孙悟空","72变");
list.add(person1);
list.add(person2);
list.add(person3);
list.add(person4);
Map<String, List<Person>> map = list.stream().collect(Collectors.groupingBy(Person::getName));
System.out.println(JSON.toJSONString(map));

2. 方式二

List<Person> list = new ArrayList<>();
Person person1 = new Person("孙悟空","筋斗云");
Person person2 = new Person("猪八戒","大师兄,师傅被妖怪抓走了");
Person person3 = new Person("沙僧","大师兄,师傅跟二师兄被妖怪抓走了");
Person person4 = new Person("孙悟空","72变");
list.add(person1);
list.add(person2);
list.add(person3);
list.add(person4);
Map<String,List<Person>> map = list.stream().collect(
     Collectors.toMap(Person::getName,each->Collections.singletonList(each),
            (value1, value2) -> {
                   List<Person> union = new ArrayList<>(value1);
                   union.addAll(value2);
                   return union;
}));
System.out.println(JSON.toJSONString(map));
图片[3]-Stream流List转成Map的几种方式-深吸氧

四、List<Object> 转成Map<String,List<String>>

List<Person> list = new ArrayList<>();
Person person1 = new Person("孙悟空","筋斗云");
Person person2 = new Person("猪八戒","大师兄,师傅被妖怪抓走了");
Person person3 = new Person("沙僧","大师兄,师傅跟二师兄被妖怪抓走了");
Person person4 = new Person("孙悟空","72变");
list.add(person1);
list.add(person2);
list.add(person3);
list.add(person4);
Map<String,List<String>> map = list.stream()
      .collect(Collectors.toMap(Person::getName,each->Collections.singletonList(each.getSkill()),
            (value1, value2) -> {
                   List<String> union = new ArrayList<>(value1);
                   union.addAll(value2);
                   return union;
}));
System.out.println(JSON.toJSONString(map));
图片[4]-Stream流List转成Map的几种方式-深吸氧

五、List<Map<String,Object>> 转成Map<String,Map<String,Object>>

List<Map<String,Object>> list = new ArrayList<>();
Person person1 = new Person("孙悟空","筋斗云");
Person person2 = new Person("猪八戒","大师兄,师傅被妖怪抓走了");
Person person3 = new Person("沙僧","大师兄,师傅跟二师兄被妖怪抓走了");
Map<String,Object> map1 = new HashMap<>();
map1.put("id","1");
map1.put("person",person1);
Map<String,Object> map2 = new HashMap<>();
map2.put("id","2");
map2.put("person",person2);
Map<String,Object> map3 = new HashMap<>();
map3.put("id","3");
map3.put("person",person3);
list.add(map1);
list.add(map2);
list.add(map3);
Map<String,Map<String,Object>> map = list.stream().collect(
     Collectors.toMap(each->Objects.toString(each.get("id"),""),each->each,(key1,key2)->key1)
);
System.out.println(JSON.toJSONString(map));
图片[5]-Stream流List转成Map的几种方式-深吸氧

六、List<Map<String,Object>> 转成Map<String,Object>

List<Map<String,Object>> list = new ArrayList<>();
Person person1 = new Person("孙悟空","筋斗云");
Person person2 = new Person("猪八戒","大师兄,师傅被妖怪抓走了");
Person person3 = new Person("沙僧","大师兄,师傅跟二师兄被妖怪抓走了");
Map<String,Object> map1 = new HashMap<>();
map1.put("id","1");
map1.put("person",person1);
Map<String,Object> map2 = new HashMap<>();
map2.put("id","2");
map2.put("person",person2);
Map<String,Object> map3 = new HashMap<>();
map3.put("id","3");
map3.put("person",person3);
list.add(map1);
list.add(map2);
list.add(map3);
Map<String,Object> map = list.stream().collect(
      Collectors.toMap(each->Objects.toString(each.get("id"),""),each->each.get("person"),(key1,key2)->key1)
);
System.out.println(JSON.toJSONString(map));
图片[6]-Stream流List转成Map的几种方式-深吸氧

七、List<Map<String,String>> 转成Map<String,Map<String,String>>

List<Map<String,String>> list = new ArrayList<>();
Map<String,String> map1 = new HashMap<>();
map1.put("id","1");
map1.put("name","孙悟空");
map1.put("address","筋斗云");
Map<String,String> map2 = new HashMap<>();
map2.put("id","2");
map2.put("name","猪八戒");
map2.put("address","大师兄,师傅被妖怪抓走了");
Map<String,String> map3 = new HashMap<>();
map3.put("id","3");
map3.put("name","沙僧");
map3.put("address","大师兄,师傅跟二师兄被妖怪抓走了");
list.add(map1);
list.add(map2);
list.add(map3);
Map<String,Map<String,String>> map = list.stream().collect(
     Collectors.toMap(each->each.get("id"),each->each,(key1,key2)->key1)
);
System.out.println(JSON.toJSONString(map));
图片[7]-Stream流List转成Map的几种方式-深吸氧

八、List<Map<String,String>> 转成Map<String,String>

List<Map<String,String>> list = new ArrayList<>();
Map<String,String> map1 = new HashMap<>();
map1.put("id","1");
map1.put("name","孙悟空");
map1.put("address","筋斗云");
Map<String,String> map2 = new HashMap<>();
map2.put("id","2");
map2.put("name","猪八戒");
map2.put("address","大师兄,师傅被妖怪抓走了");
Map<String,String> map3 = new HashMap<>();
map3.put("id","3");
map3.put("name","沙僧");
map3.put("address","大师兄,师傅跟二师兄被妖怪抓走了");
list.add(map1);
list.add(map2);
list.add(map3);
Map<String,String> map = list.stream().collect(
     Collectors.toMap(each->each.get("id"),each->each.get("name"),(key1,key2)->key1)
);
System.out.println(JSON.toJSONString(map));
图片[8]-Stream流List转成Map的几种方式-深吸氧
© 版权声明
THE END
请撒泡尿证明你到此一游
点赞6 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容