1.只需要list的某两个值。作为map的key和value——map

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

    2.id作为主键,value是对象。——map

    1. public Map<Long, Account> getIdAccountMap(List<Account> accounts) {
    2. return accounts.stream().collect(Collectors.toMap(Account::getId, account -> account));
    3. }

    3.list按照某个字段分组。——map>

    1. Map<String, List<InfoDto>> map = list.stream().collect(Collectors.groupingBy(InfoDto::getId));