java 8 - Why do I get compilation error when doing flatmap() call? -
let's have following object:
public class daylydata { private date date; private list<integer> numerslist; // standard getters/setters public map<integer, date> getinttodate() { map<integer, date> resultmap = new hashmap<>(); for(integer number : getnumerslist()) { resultmap.put(number, getdate()); } return resultmap; } now, let have list of daylydata: list<daylydata> resultlist.
what result of following:
resultlist.stream().flatmap(entity -> entity.getinttodate()); if assign result of stream<map<integer, date>>, getting type mismatch: cannot convert map<integer,date> stream<? extends map<integer,date>>.
thanks in advance.
the flatmap method special case of map , used flattening nested stream, optional, , other monadic tools.
in case, not providing function returns stream can't used flatmap param.
your function work fine standard map(), though:
resultlist.stream() .map(entity -> entity.getinttodate()); // no compilation errors you make example work wrapping result in stream instance not give advantage on example above - makes sense educational purposes:
resultlist.stream() .flatmap(entity -> stream.of(entity.getinttodate())); // no compilation error it's "daily" not "dayly".
Comments
Post a Comment