通过JAVA8的流操作需要转换成userId为key, name为value的map。
public class User { private Integer userId; private String name; private String email; public User(Integer userId, String name, String email) { this.userId = userId; this.name = name; this.email = email; } public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "User{" + "userId=" + userId + ", name='" + name + ''' + ", email='" + email + ''' + '}'; } } 复制代码
public class ListToMap { public static void main(String[] args) { Listusers = new ArrayList<>(); users.add(new User(1, "user1", "[email protected]")); users.add(new User(2, "user2", "[email protected]")); users.add(new User(3, "user3", "[email protected]")); users.add(new User(4, "user4", "[email protected]")); Map userIdAndName = users.stream() .collect(Collectors.toMap(User::getUserId, User::getName)); System.out.println(userIdAndName); } } 复制代码
输出结果
public class ListToMap { public static void main(String[] args) { Listusers = new ArrayList<>(); users.add(new User(1, "user1", "[email protected]")); users.add(new User(2, "user2", "[email protected]")); users.add(new User(3, "user3", "[email protected]")); users.add(new User(4, "user4", "[email protected]")); Map userIdAndUser = users.stream() .collect(Collectors.toMap(User::getUserId, user-> user)); System.out.println(userIdAndUser); } } 复制代码
输出
在转换过程中,如果出现相同的key,那么会抛出重复key的异常。
我们将userId为3的修改为2,和第二个用户冲突
Collectors.toMap支持第三个参数,对重复值进行选取
public class ListToMap { public static void main(String[] args) { Listusers = new ArrayList<>(); users.add(new User(1, "user1", "[email protected]")); users.add(new User(2, "user2", "[email protected]")); users.add(new User(2, "user3", "[email protected]")); users.add(new User(4, "user4", "[email protected]")); Map userIdAndUser = users.stream() .collect(Collectors.toMap(User::getUserId, user-> user, (oldValue, newValue) -> newValue)); System.out.println(userIdAndUser); } } 复制代码
选取新的值覆盖旧的值,得到的结果
只会出现user3,而user2被覆盖掉了。
刚才上面出现重复的ID,是根据值进行覆盖,如果在某些情况下需要映射成列表。
即:List -> Map
这需要使用到groupingBy
public class ListToMap { public static void main(String[] args) { Listusers = new ArrayList<>(); users.add(new User(1, "user1", "[email protected]")); users.add(new User(2, "user2", "[email protected]")); users.add(new User(2, "user3", "[email protected]")); users.add(new User(4, "user4", "[email protected]")); Map > userMap = users.stream() .collect(Collectors.groupingBy(User::getUserId)); System.out.println(userMap); } } 复制代码
ID为2的出现多条数据。
Collectors.toMap支持第四个参数,对map类型进行实例化
public class ListToMap { public static void main(String[] args) { Listusers = new ArrayList<>(); users.add(new User(1, "user1", "[email protected]")); users.add(new User(2, "user2", "[email protected]")); users.add(new User(3, "user3", "[email protected]")); users.add(new User(4, "user4", "[email protected]")); Map userIdAndUser = users.stream() .collect(Collectors.toMap(User::getUserId, user-> user, (oldValue, newValue) -> newValue, ConcurrentHashMap::new)); System.out.println(userIdAndUser); System.out.println(userIdAndUser.getClass()); } } 复制代码
输出
userIdAndUser类的类型是ConcurrentHashMap