Java中List使用stream流转成map的几种方式详解
Stream是Java8中处理集合的关键抽象概念,它可以指定你希望对集合进行的操作,可以执行非常复杂的查找、过滤和映射数据等操作,下面这篇文章主要给大家介绍了关于Java中List使用stream流转成map的几种方式,需要的朋友可以参考下
Java技术迷
实体例子
public class Person { private String name; private String address; public Person(String name, String address) { this.name = name; this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
List 转成Map
Listlist = new ArrayList<>(); Person person1 = new Person("熊大","森林第一个小屋"); Person person2 = new Person("熊二","森林第二个小屋"); Person person3 = new Person("光头强","森林第三个小屋"); Person person4 = new Person("熊大","森林第四个小屋"); list.add(person1); list.add(person2); list.add(person3); list.add(person4); Map map = list.stream().collect(Collectors.toMap(Person::getName,each->each,(value1, value2) -> value1)); System.out.println(JSON.toJSONString(map));
控制台打印日志:
{“光头强”:{“address”:“森林第三个小屋”,“name”:“光头强”},“熊大”:{“address”:“森林第一个小屋”,“name”:“熊大”},“熊二”:{“address”:“森林第二个小屋”,“name”:“熊二”}}
List 转成Map
Listlist = new ArrayList<>(); Person person1 = new Person("熊大","森林第一个小屋"); Person person2 = new Person("熊二","森林第二个小屋"); Person person3 = new Person("光头强","森林第三个小屋"); Person person4 = new Person("熊大","森林第四个小屋"); list.add(person1); list.add(person2); list.add(person3); list.add(person4); Map map = list.stream().collect(Collectors.toMap(Person::getName,Person::getAddress,(value1, value2) -> value1)); System.out.println(JSON.toJSONString(map));
控制台打印日志:
{“光头强”:“森林第三个小屋”,“熊大”:“森林第一个小屋”,“熊二”:“森林第二个小屋”}
List 转成Map
方法一:
Listlist = new ArrayList<>(); Person person1 = new Person("熊大","森林第一个小屋"); Person person2 = new Person("熊二","森林第二个小屋"); Person person3 = new Person("光头强","森林第三个小屋"); Person person4 = new Person("熊大","森林第四个小屋"); list.add(person1); list.add(person2); list.add(person3); list.add(person4); Map > map = list.stream().collect(Collectors.groupingBy(Person::getName)); System.out.println(JSON.toJSONString(map));
控制台打印日志:
{“光头强”:[{“address”:“森林第三个小屋”,“name”:“光头强”}],“熊大”:[{“address”:“森林第一个小屋”,“name”:“熊大”},{“address”:“森林第四个小屋”,“name”:“熊大”}],“熊二”:[{“address”:“森林第二个小屋”,“name”:“熊二”}]}
方法二:
Listlist = new ArrayList<>(); Person person1 = new Person("熊大","森林第一个小屋"); Person person2 = new Person("熊二","森林第二个小屋"); Person person3 = new Person("光头强","森林第三个小屋"); Person person4 = new Person("熊大","森林第四个小屋"); list.add(person1); list.add(person2); list.add(person3); list.add(person4); Map > map = list.stream().collect(Collectors.toMap(Person::getName,each->Collections.singletonList(each),(value1, value2) -> { List union = new ArrayList<>(value1); union.addAll(value2); return union; })); System.out.println(JSON.toJSONString(map));
控制台打印日志:
{“光头强”:[{“address”:“森林第三个小屋”,“name”:“光头强”}],“熊大”:[{“address”:“森林第一个小屋”,“name”:“熊大”},{“address”:“森林第四个小屋”,“name”:“熊大”}],“熊二”:[{“address”:“森林第二个小屋”,“name”:“熊二”}]}
List 转成Map
Listlist = new ArrayList<>(); Person person1 = new Person("熊大","森林第一个小屋"); Person person2 = new Person("熊二","森林第二个小屋"); Person person3 = new Person("光头强","森林第三个小屋"); Person person4 = new Person("熊大","森林第四个小屋"); list.add(person1); list.add(person2); list.add(person3); list.add(person4); Map > map = list.stream().collect(Collectors.toMap(Person::getName,each->Collections.singletonList(each.getAddress()),(value1, value2) -> { List union = new ArrayList<>(value1); union.addAll(value2); return union; })); System.out.println(JSON.toJSONString(map));
控制台打印日志:
{“光头强”:[“森林第三个小屋”],“熊大”:[“森林第一个小屋”,“森林第四个小屋”],“熊二”:[“森林第二个小屋”]}
List
List
控制台打印日志:
{“1”:{“person”:{“address”:“森林第一个小屋”,“name”:“熊大”},“id”:“1”},“2”:{“person”:{“address”:“森林第二个小屋”,“name”:“熊二”},“id”:“2”},“3”:{“person”:{“address”:“森林第三个小屋”,“name”:“光头强”},“id”:“3”}}
List
List
控制台打印日志:
{“1”:{“address”:“森林第一个小屋”,“name”:“熊大”},“2”:{“address”:“森林第二个小屋”,“name”:“熊二”},“3”:{“address”:“森林第三个小屋”,“name”:“光头强”}}
List
List
控制台打印日志:
{“1”:{“address”:“森林第一个小屋”,“name”:“熊大”,“id”:“1”},“2”:{“address”:“森林第二个小屋”,“name”:“熊二”,“id”:“2”},“3”:{“address”:“森林第三个小屋”,“name”:“光头强”,“id”:“3”}}
List
List
控制台打印日志:
{“1”:“熊大”,“2”:“熊二”,“3”:“光头强”}