set of Stream stream operations using Java8
List<String> fruitList=Stream
.of("apple","banana","coconut","Damson","Filbert","Lemon")
.collect(Collectors.toList());
List<String> meatList=Stream
.of("beef","pork","chicken","lamp","oyster","salmon","tuna")
.collect(Collectors.toList());
List<List<?>> bigList=Stream.of(fruitList,meatList).collect(Collectors.toList());
I want to flatten the collection using the flatMap method and then use the longest string of the reduce specification
bigList.stream().flatMap(x->x.stream()
.reduce((String x,String y)->x.length()>y.length()?x:y)
.ifPresent(System.out::println);
result error
error message is shown in figure
what is this error capture of?
I haven"t seen any Stream article about
if you change the code to
bigList.stream().flatMap(x->x.stream())
.map(x->x+"")
.reduce((String x,String y)->x.length()>y.length()?x:y)
.ifPresent(System.out::println);
that"s fine
if
bigList.stream().flatMap(x->x.stream()).forEach(System.out::println)
this line of code also works normally.
all the elements after merging the two collections in turn bigList.stream (). FlatMap (x-> x.stream ())
should return
the collection of all elements
Why can"t I use the reduce method directly later?