[Solved] Java 8 – collecting into a map from a list of objects with nested list


Assuming you want a Map<User.userId, List<UserData.customerId>> you can use this:

 Map<Long, List<Long>> result = users.stream()
    .collect(Collectors.toMap(
        User::getUserId,
        u -> u.getUserData().stream()
            .map(UserData::getCustomerId)
            .collect(Collectors.toList())
    ));

3

solved Java 8 – collecting into a map from a list of objects with nested list