Flatmap in java8 Stream cannot be used with reduce?

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?

Mar.28,2021

The main reason for reporting an error in

is actually because of the parameters needed in the flatMap method. You can see that it is actually a Function ,

.

clipboard.png

FunctionStreams.stream()..s.streamreducereduceOptional

clipboard.png

Of course,

does not meet the requirements, so the compilation error

of course, if the subject finally combines the two stream together, you can not use the Stream.of method. You can consider ha, since the types in the previous two stream are the same, you can directly use Stream.contact , so you don't have to flatten it out. It's just flat

.
Stream.concat(fruitList.stream(), meatList.stream())

mainly because your biglist is list? Can only capture variables, not call methods

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e44834-44aac.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e44834-44aac.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?