user565
user565

Reputation: 1001

How to get the Collection using stream API

I am trying to access a list of string from a data structure which is having collection of collection. What I have tried yet is returning a list of collection,but I want a single list. Here is my code:

    Collection result= (Collection)allPatientsAccessRecords.stream()
            .filter(rec -> rec.data.personId==8191).map(data->data.accessData.associatedAreas).
                    collect(Collectors.toList());

Here is a snapshot of a source data structure: enter image description here

Can someone hint me how i can get accessData->associatedArea(list of string) as a result using stream API ?

Upvotes: 1

Views: 97

Answers (2)

Michal
Michal

Reputation: 1010

This should get you started:

List<List<String>> example = ... ;
example.stream().flatMap(list->list.stream()).collect(...)

Upvotes: 1

Thiyagu
Thiyagu

Reputation: 17890

Use flatMap

allPatientsAccessRecords.stream()
        .filter(rec -> rec.data.personId == 8191)
        .flatMap(data -> data.accessData.associatedAreas.stream())
        .collect(Collectors.toList());

Upvotes: 3

Related Questions