- 我们有一个用户信息类
通过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) { List<User> users = new ArrayList<>(); users.add(new User(1, "user1", "email1@demo.com")); users.add(new User(2, "user2", "email2@demo.com")); users.add(new User(3, "user3", "email3@demo.com")); users.add(new User(4, "user4", "email4@demo.com")); Map<Integer, String> userIdAndName = users.stream() .collect(Collectors.toMap(User::getUserId, User::getName)); System.out.println(userIdAndName); } } 复制代码
输出结果
- userId为key,用户对象为value
public class ListToMap { public static void main(String[] args) { List<User> users = new ArrayList<>(); users.add(new User(1, "user1", "email1@demo.com")); users.add(new User(2, "user2", "email2@demo.com")); users.add(new User(3, "user3", "email3@demo.com")); users.add(new User(4, "user4", "email4@demo.com")); Map<Integer, User> userIdAndUser = users.stream() .collect(Collectors.toMap(User::getUserId, user-> user)); System.out.println(userIdAndUser); } } 复制代码
输出
- 重复的key处理
在转换过程中,如果出现相同的key,那么会抛出重复key的异常。
我们将userId为3的修改为2,和第二个用户冲突
Collectors.toMap支持第三个参数,对重复值进行选取
public class ListToMap { public static void main(String[] args) { List<User> users = new ArrayList<>(); users.add(new User(1, "user1", "email1@demo.com")); users.add(new User(2, "user2", "email2@demo.com")); users.add(new User(2, "user3", "email3@demo.com")); users.add(new User(4, "user4", "email4@demo.com")); Map<Integer, User> userIdAndUser = users.stream() .collect(Collectors.toMap(User::getUserId, user-> user, (oldValue, newValue) -> newValue)); System.out.println(userIdAndUser); } } 复制代码
选取新的值覆盖旧的值,得到的结果
只会出现user3,而user2被覆盖掉了。
- 将ID和用户列表进行映射
刚才上面出现重复的ID,是根据值进行覆盖,如果在某些情况下需要映射成列表。
即:List -> Map<Integer, List>的情况
这需要使用到groupingBy
public class ListToMap { public static void main(String[] args) { List<User> users = new ArrayList<>(); users.add(new User(1, "user1", "email1@demo.com")); users.add(new User(2, "user2", "email2@demo.com")); users.add(new User(2, "user3", "email3@demo.com")); users.add(new User(4, "user4", "email4@demo.com")); Map<Integer, List<User>> userMap = users.stream() .collect(Collectors.groupingBy(User::getUserId)); System.out.println(userMap); } } 复制代码
ID为2的出现多条数据。
- 指定Map类型
Collectors.toMap支持第四个参数,对map类型进行实例化
public class ListToMap { public static void main(String[] args) { List<User> users = new ArrayList<>(); users.add(new User(1, "user1", "email1@demo.com")); users.add(new User(2, "user2", "email2@demo.com")); users.add(new User(3, "user3", "email3@demo.com")); users.add(new User(4, "user4", "email4@demo.com")); Map<Integer, User> 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
相关文章
暂无评论...