How to return List from Java 16 Stream.map()?

121 Views Asked by At

I am iterating over a list, calling dynamoDBMapper.query() which return a PaginatedQueryList.

public List<Person> getPerson(final List<UUID> personIds) {
        return personIds.stream().map(personId ->  dynamoDBMapper.query(Person.class, findByPersonId(personId)).stream()
            .map(Person::getPerson))
            .collect(toList());
}

I need to return List but the error I am getting is :

Required type:
List<Person>
Provided:
List<Stream<Person>>

Any help will be appreciated.

1

There are 1 best solutions below

1
Michael Gantman On

This a a quote from javadoc for class Collectors

// Accumulate names into a List
     List<String> list = people.stream().map(Person::getName).collect(Collectors.toList());

Just few clarifying questions: what does method Person::getPerson return? is it instance of a class Person (that what I would expect for your code to work properly) or is it a Stream? Also in your code what does method toList() do?