1.只需要list的某两个值。作为map的key和value——map
public Map<Long, String> getIdNameMap(List<Account> accounts) {
return accounts.stream().collect(Collectors.toMap(Account::getId, Account::getUsername));
}
2.id作为主键,value是对象。——map
public Map<Long, Account> getIdAccountMap(List<Account> accounts) {
return accounts.stream().collect(Collectors.toMap(Account::getId, account -> account));
}
3.list按照某个字段分组。——map
Map<String, List<InfoDto>> map = list.stream().collect(Collectors.groupingBy(InfoDto::getId));