functional programming - Have to work with object which has Set of enties using Java 8? -
i have implement method following signature:
public set<event> getfordaterange(calendar from, calendar to)
my event
class looks like:
public class event { private integer id; private string name; private integer price; private rating rating; private set<calendar> eventdatetime;
i can not find how datetimes
event. here have far:
public set<event> getfordaterange(calendar from, calendar to) { set<event> result = collections.emptyset(); set<event> allevents = getall(); (event event : allevents) { set<calendar> eventdatetime = event.geteventdatetime(); result.addall(eventdatetime.stream() .filter(date -> date.after(from) && date.before(to)) .map(date -> event) .collect(collectors.toset())); } return result; }
i using mixture of iterative style functional. how write method using functional style only?
your current code bit weird: .map(date -> event)
mapping dates same event.
what want retain events such 1 of event time between from
, to
. can obtain anymatch
. operation returns whether of stream element matches given predicate. in case, if dates between from
, to
, keep event , collect set
.
public set<event> getfordaterange(calendar from, calendar to) { set<event> allevents = getall(); return allevents.stream() .filter(event -> event.geteventdatetime() .stream() .anymatch(date -> date.after(from) && date.before(to)) ) .collect(collectors.toset()); }
Comments
Post a Comment