FlatMap () to convert a LinkedList <String> stream to a String stream

My goal is exactly what the title says. What am I doing:

.stream().flatMap(x -> x.getTitles()) 

getTitles() returns a LinkedList<String> , and I expected flatMap() to do the job and create a stream of strings instead of a LinkedList<String> , but Eclipse says:

Type mismatch: cannot convert from LinkedList<String> to Stream<? extends Object> Stream<? extends Object>

How can i do this? (I need to do this with threads, it's all part of a larger thread)

+5
source share
1 answer

flatMap expects matching to a stream, not a collection. Use

 .stream().flatMap(x -> x.getTitles().stream()) // ^^^^^^^^ add this 
+7
source

All Articles