0

I have POJO class:

@Data
@AllArgsConstructor
public class Person {
    private String name;
    private String surname;
}

I have some executable code:

public class Main {

    public static void main(String[] args) {
        Person john1 = new Person("John", "Smith");
        Person john2 = new Person("John", "Brown");
        Person nancy1 = new Person("Nancy", "James");
        Person nancy2 = new Person("Nancy", "Williams");
        Person kate1 = new Person("Kate", "Fletcher");

        List<Person> persons = List.of(john1, kate1, john2, nancy1, nancy2);
        Map<String, List<Person>> result = persons.stream().collect(Collectors.groupingBy(Person::getName));
        System.out.println(result);
    }
}

How can I get Stream<List<Person>> instead Map<String, List<Person>> into result? and don't need keys. Can I get it without Map-collection using?

UPD: There are persons with the same name in every list

  • 2
    if you want the lists of persons with same name: `result.values()` - add a `.stream()` if needed as stream (but the question is a bit unclear, only required type is given, not what data is expected) – user16320675 Nov 23 '21 at 07:30
  • @user16320675 Thanks for your answer. I need stream with listst of persons, where persons with the same name are in every list. Your decision is suitable, but at the end of my question I wrote, if it is posible without map usage – Oleg Sandro Nov 23 '21 at 07:41
  • Why do you not want to use a map? It would be quite slow asymptotically without a map. – Sweeper Nov 23 '21 at 07:43
  • @OlegSandro, if you need to group the persons by some field/property, collecting into a map is required. – Nowhere Man Nov 23 '21 at 07:44
  • 1
    well, you can write a loop an use only lists, but what else should not be used? why? (Please [edit] the question and the missing information like list with persons with same name, what is not *allowed* to be used, ....) - anyway if you want a stream of lists, the lists must be saved in some collection first – user16320675 Nov 23 '21 at 07:53
  • @AlexRudenko, @ Sweeper, I thought that there is more effective way to do that without Map-usage. Because we have deals with stream, then with collection, and finally with stream again. I thought that there is any way to convert first stream to final stream. It seems that there is no such way. Thanks a lot for your help – Oleg Sandro Nov 23 '21 at 08:03
  • @user16320675 Thank you! I added to question description info about same name persons in every list. At the current implementation Map values store exactly that info I need. In the end of question is specified that I don't want to apply Map usage. This condition is pointed out because I suspected there was a better way. This was the subject of my question. Now I see, that there is no better solution that using Map – Oleg Sandro Nov 23 '21 at 09:49

1 Answers1

3

Upon building the map, return the stream for the Collection<List<Person>> retrieved by Map::values method:

Stream<List<Person>> stream = persons.stream()
        .collect(Collectors.groupingBy(Person::getName))
        .values() // Collection<List<Person>>
        .stream(); // Stream<List<Person>> 
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
  • Thanks. It is useful. But is there another solution to get the stream of lists with same name persons without Map-collection usage? Because we have stream, next we have collection and finally we have stream again. I think maybe we can do that without conversion to collection – Oleg Sandro Nov 23 '21 at 07:50
  • We have `Stream`, which could be mapped into `Stream>` where each list contains only one `Person`, but then these lists need to be joined/merged somehow to combine the persons with the same name, _therefore_ intermediate collection and appropriate terminal operation is required. – Nowhere Man Nov 23 '21 at 08:13