Reputation: 173
ColumnFamily column = tokens.getColumnFamilies().stream()
.filter(family -> family.getName().equals("this_family"))
.findAny()
.get();
Map<String, String> tokenized = column.getColumns().stream()
.collect(Collectors.toMap(
Column::getQualifier,
Column::getValue
));
Is there a way I can combine these two streams into one? I am using the first stream to filter and find on my nested list, and using the second stream to create a map based on the result of the stream. I am wondering if there's a way I could have done this using a single stream.
Something like this
Map<String, String> tokenized = tokens.getColumnFamilies().stream()
.filter(family -> family.getName().equals("this_family"))
.collect(Collectors.toMap(
//
));
Upvotes: 1
Views: 679
Reputation: 18430
Use Optional#map()
and combine
Map<String, String> tokenized = tokens.getColumnFamilies().stream()
.filter(family -> family.getName().equals("this_family"))
.findAny()
.map(cf -> cf.getColumns().stream()).get()
.collect(Collectors.toMap(Column::getQualifier, Column::getValue));
Upvotes: 0
Reputation: 26046
You can use flatMap
to get nested Stream
and flatten the structure:
Map<String, String> tokenized = tokens.getColumnFamilies().stream()
.filter(family -> family.getName().equals("this_family"))
.limit(1) // equivalent of findAny
.flatMap(cf -> cf.getColumns().stream())
.collect(Collectors.toMap(
Column::getQualifier,
Column::getValue
));
Upvotes: 2