2

Подскажите как преобразовать:

Map<String, List<Pet>> pets = petList
            .stream()
            .collect(Collectors.groupingBy(Pet::getType));

в

Map<String, PetsHouse>

при помощи stream? Или я сильно заморачиваюсь и решить без использования стримов?

PetHouse - это просто некая обертка над листом

List<Pet> petList;
PetHouse house = new PetHouse(petList);
0

2 ответа 2

5
Map<String, PetHouse> m = pets.entrySet().stream()
            .collect(Collectors.toMap(Entry::getKey, e -> new PetHouse(e.getValue())));
1
  • Спасибо, так я и решил! Просто я думал что можно както этот маппинг провести еще на уровне группировки
    – JVic
    10 янв 2018 в 13:29
4

Всё решаемо:

Map<String, List<Pet>> pets = petList
        .stream()
        .collect(Collectors.groupingBy(
            Pet::getType,
            Collectors.collectingAndThen(Collectors.toList(), PetHouse::new)
        ));
1
  • 1
    Вот, вот то что я хотел!! Спасибо!
    – JVic
    10 янв 2018 в 13:43

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.