Skip to content

Latest commit

 

History

History
54 lines (41 loc) · 1.64 KB

File metadata and controls

54 lines (41 loc) · 1.64 KB

Java8 使用 streamList 转为 Map

Collectors.toMap

public Map<Long, String> getIdNameMap(List<Account> accounts) {
    return accounts.stream().collect(Collectors.toMap(Account::getId, Account::getUsername));
}

收集对象实体本身

public Map<Long, Account> getIdAccountMap(List<Account> accounts) {
    return accounts.stream().collect(Collectors.toMap(Account::getId, account -> account));
    // or
    return accounts.stream().collect(Collectors.toMap(Account::getId, Function.identity()));
}

重复 key 问题

转换中 key 值重复时,流的处理会抛出异常:Java.lang.IllegalStateException:Duplicate key

处理方式:(指定 key 重复时的处理方式, toMap 的第三个参数)

public Map<Long, Account> getIdAccountMap(List<Account> accounts) {
    // 保留旧值
    return accounts.stream().collect(Collectors.toMap(Account::getId, Function.identity(), (k1, k2) -> k1));
    // 使用新值
    return accounts.stream().collect(Collectors.toMap(Account::getId, Function.identity(), (k1, k2) -> k2));
}

分组

public Map<Integer, List<Person>> getAgePersonMap(List<Person> persons) {
    return persons.stream().collect(Collectors.groupingBy(Person::getAge));
}

条件分组

public Map<Boolean, List<Person>> getAgePersonMap(List<Person> persons) {
    // 特殊的groupingBy,根据条件分为两组
    return persons.stream().collect(Collectors.partitioningBy(person -> person.getAge() < 18));
}