java stream 将List转换成Map

时间:2025-04-02 14:25:39
userList.stream().collect(Collectors.toMap(User::getId, User::getName)); userList.stream().collect(Collectors.toMap(User::getId, t -> t)); for (User user : list) { Set<Integer> addressesIds = addressesList.stream().map(Address::getId).collect(Collectors.toSet()); if (addressesIds.contains(user.getAddress())){ String s = addressMap.get(user .getAddress()); user .setAddressName(s); } }