java8 stream转map操作
1 package com.example.mass_study.test01.anything; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import java.util.Map; 6 import java.util.concurrent.ConcurrentHashMap; 7 import java.util.function.Function; 8 import java.util.function.Predicate; 9 import java.util.stream.Collectors; 10 11 /** 12 * @description: 13 * @author: luguilin 14 * @date: 2022-04-13 11:17 15 **/ 16 17 18 public class StreamToMapTest { 19 public static void main(String[] args) { 20 List<User> users = new ArrayList<>(); 21 User user1 = new User(1, "1"); 22 User user2 = new User(2, "2"); 23 User user3 = new User(3, "3"); 24 users.add(user1); 25 users.add(user2); 26 users.add(user3); 27 // 将id属性读取出来组成list 28 List<Integer> idList = users.stream().map(User::getId).collect(Collectors.toList()); 29 System.out.println(idList); 30 31 // 将id属性读取出来组成list 并去重 32 List<Integer> idList2 = users.stream().map(User::getId).distinct().collect(Collectors.toList()); 33 System.out.println(idList2); 34 35 // 组装成map 36 Map<Integer, String> mp1 = users.stream().collect(Collectors.toMap(User::getId, User::getName)); 37 System.out.println(mp1); 38 39 User user3_1 = new User(3, "3-1"); 40 users.add(user3_1); 41 42 // 组装成map 43 // key 重复会报错 44 // Map<Integer, String> mp2 = users.stream().collect(Collectors.toMap(User::getId, User::getName)); 45 // System.out.println(mp1); 46 47 // 组装成map 值取前面的 48 Map<Integer, String> mp3 = users.stream().collect(Collectors.toMap(User::getId, User::getName, (v1, v2) -> v1)); 49 System.out.println(mp3); 50 51 // 组装成map 值取后面的 52 Map<Integer, String> mp2 = users.stream().collect(Collectors.toMap(User::getId, User::getName, (v1, v2) -> v2)); 53 System.out.println(mp2); 54 55 // 组装成map 值取累加 56 Map<Integer, String> mp4 = users.stream().collect(Collectors.toMap(User::getId, User::getName, (v1, v2) -> v1+v2)); 57 System.out.println(mp4); 58 59 // 根据id 组装list对象,对应id的对象成组 60 Map<Integer, List<User>> mp5 = users.stream().collect(Collectors.groupingBy(User::getId)); 61 System.out.println(mp5); 62 63 // 根据id 组装list对象,对应id的对象成组,如果确认对象只有一个, 64 Map<Integer, User> mp8 = users.stream().collect(Collectors.toMap(User::getId, Function.identity(), (o1, o2) -> o2)); 65 System.out.println(mp8); 66 67 // 根据id 组装list对象,对应id的对象的某个属性成组 68 Map<Integer,List<String>> mp6 = users.stream().collect(Collectors.groupingBy(User::getId, Collectors.mapping(User::getName, Collectors.toList()))); 69 System.out.println(mp6); 70 71 Map<Integer, List<String>> mp7 = users.stream().collect(Collectors.toMap(User::getId, 72 p -> { 73 List<String> getNameList = new ArrayList<>(); 74 getNameList.add(p.getName()); 75 return getNameList; 76 }, 77 (List<String> value1, List<String> value2) -> { 78 value1.addAll(value2); 79 return value1; 80 } 81 )); 82 System.out.println(mp7); 83 84 User user4 = new User(4, null); 85 users.add(user4); 86 87 // 属性为空值,也可以输出 88 Map<Integer, List<String>> mp8 = users.stream().collect(Collectors.toMap(User::getId, 89 p -> { 90 List<String> getNameList = new ArrayList<>(); 91 getNameList.add(p.getName()); 92 return getNameList; 93 }, 94 (List<String> value1, List<String> value2) -> { 95 value1.addAll(value2); 96 return value1; 97 } 98 )); 99 System.out.println(mp8); 100 101 // 以对象为值 102 Map<Integer, User> mp9 = users.stream() 103 .filter(distinctByKey(User::getId)) 104 .collect(Collectors.toMap(User::getId, User -> User)); 105 System.out.println(mp9); 106 } 107 108 public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) { 109 Map<Object,Boolean> seen = new ConcurrentHashMap<>(); 110 return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null; 111 } 112 } 113 114 class User{ 115 private Integer id; 116 private String name; 117 118 public User() { 119 } 120 121 public User(Integer id, String name) { 122 this.id = id; 123 this.name = name; 124 } 125 126 public Integer getId() { 127 return id; 128 } 129 130 public void setId(Integer id) { 131 this.id = id; 132 } 133 134 public String getName() { 135 return name; 136 } 137 138 public void setName(String name) { 139 this.name = name; 140 } 141 }