1.List 转成Map<String,Bean>
bean的情况
List<Person> list = 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<String,Person> 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”:“熊二”}}
map的情况
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));
控制台打印日志:
{“1”:{“address”:“森林第一个小屋”,“name”:“熊大”,“id”:“1”},“2”:{“address”:“森林第二个小屋”,“name”:“熊二”,“id”:“2”},“3”:{“address”:“森林第三个小屋”,“name”:“光头强”,“id”:“3”}}
2.List 转成Map<String,String>
bean的情况
List<Person> list = 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<String,String> map = list.stream().collect(Collectors.toMap(Person::getName,Person::getAddress,(value1, value2) -> value1));
System.out.println(JSON.toJSONString(map));
控制台打印日志:
{“光头强”:“森林第三个小屋”,“熊大”:“森林第一个小屋”,“熊二”:“森林第二个小屋”}
map的情况
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));
控制台打印日志:
{“1”:“熊大”,“2”:“熊二”,“3”:“光头强”}
3.List 转成Map<String,List(Bean)>(分组)
方法一:
List<Person> list = 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<String, List<Person>> map = list.stream().collect(Collectors.groupingBy(Person::getName));
System.out.println(JSON.toJSONString(map));
控制台打印日志:
{“光头强”:[{“address”:“森林第三个小屋”,“name”:“光头强”}],“熊大”:[{“address”:“森林第一个小屋”,“name”:“熊大”},{“address”:“森林第四个小屋”,“name”:“熊大”}],“熊二”:[{“address”:“森林第二个小屋”,“name”:“熊二”}]}
方法二:
List<Person> list = 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<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));
控制台打印日志:
{“光头强”:[{“address”:“森林第三个小屋”,“name”:“光头强”}],“熊大”:[{“address”:“森林第一个小屋”,“name”:“熊大”},{“address”:“森林第四个小屋”,“name”:“熊大”}],“熊二”:[{“address”:“森林第二个小屋”,“name”:“熊二”}]}
4.List 转成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("熊大","森林第四个小屋");
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.getAddress()),(value1, value2) -> {
List<String> union = new ArrayList<>(value1);
union.addAll(value2);
return union;
}));
System.out.println(JSON.toJSONString(map));
控制台打印日志:
{“光头强”:[“森林第三个小屋”],“熊大”:[“森林第一个小屋”,“森林第四个小屋”],“熊二”:[“森林第二个小屋”]}
5.List转List
提取对象列表中的某个属性列表
List<属性值类型> 属性List = list.stream().map(对象:get方法()).collect(Collectors.toList())):
例如:
List<Integer> idList = list.stream().map(User:getld).collect(Collectors.toList()):
List<Integer> idList = list.stream().map(u -> u.getld()).collect(Collectors.toList()):
参考:https://www.jb51.net/article/281570.htm#_label8