Java8流List转Map

  • 我们有一个用户信息类

通过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 users = 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);
    }
}
复制代码

输出结果

Java8流List转Map_第1张图片

  • userId为key,用户对象为value
public class ListToMap {
    public static void main(String[] args) {
        List users = 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,那么会抛出重复key的异常。

我们将userId为3的修改为2,和第二个用户冲突

Collectors.toMap支持第三个参数,对重复值进行选取

Java8流List转Map_第2张图片

public class ListToMap {
    public static void main(String[] args) {
        List users = 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和用户列表进行映射

刚才上面出现重复的ID,是根据值进行覆盖,如果在某些情况下需要映射成列表。

即:List -> Map的情况

这需要使用到groupingBy

public class ListToMap {
    public static void main(String[] args) {
        List users = 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的出现多条数据。

  • 指定Map类型

Collectors.toMap支持第四个参数,对map类型进行实例化

Java8流List转Map_第3张图片

public class ListToMap {
    public static void main(String[] args) {
        List users = 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

你可能感兴趣的:(Java,程序员,编程,蓝桥杯,java,linq)