How to convert java 8 map, filter and streams to scala? -
i new scala trying understand changing equivalent java scala better understanding.
how convert java 8 map, filter , streams scala ?
i have following java 8 code trying convert scala :
public set<string> getvalidusages(string itemid, long sno, date timeofaccess) { set<string> itemset = sets.newhashset(); testwindows testwindows = items.get(itemid).gettestwindows(); final boolean istv = existseligibletestwindow(testwindows.gettv(), timeofaccess); if (istv) { itemset.add(tv); } else { final boolean iscableuseable = existseligibletestwindow(testwindows.getcableuse(), timeofaccess); final boolean iswifi = existseligibletestwindow(testwindows.getwifi(), timeofaccess); if (iscableuseable || iswifi) { itemset.add(movie); } } if (testwindows.getusageids() != null) { itemset.addall(testwindows.getusageids() .entryset() .stream() .filter(entry -> existseligibletestwindow(entry.getvalue(), timeofaccess)) .map(map.entry::getkey) .collect(collectors.toset())); } return itemset; } private boolean existseligibletestwindow(list<testwindow> windows, date timeofaccess) { if (windows != null) { return windows.stream() .filter(w -> withindaterange(timeofaccess, w)) .findany() .ispresent(); } return false; } private boolean withindaterange(date tocheck, testwindow window) { return tocheck.after(window.getstarttime()) && tocheck.before(window.getendtime()); }
i tried :
def withindaterange(tocheck: date, window: testwindow): boolean = { tocheck.after( window.getstarttime ) && tocheck.before( window.getendtime ) } def getvalidusages(itemid: string, sno: long, timeofaccess: date): set[string] = { var itemset = sets.newhashset() val testwindows = items.value(itemid).gettestwindows val istv = existseligibletestwindow(testwindows.get(0).gettv, timeofaccess) if (istv) { itemset += tv } else { val iscableuseable = existseligibletestwindow(testwindows.get(0).getcableuse, timeofaccess) val iswifi = existseligibletestwindow(testwindows.get(0).getwifi, timeofaccess) if (iscableuseable || iswifi) { itemset += movie } } if (testwindows.get(0).getusageids != null) { itemset.addall(testwindows.get(0).getusageids.entryset().stream() .filter((x) => existseligibletestwindow(x._2, timeofaccess)).map(x => map.entry._1 ) .collect(collectors.toset())) } itemset } def existseligibleconsumptionwindow(windows: list[consumptionwindow], timeofaccess: date): boolean = { if (windows != null) { return windows.exists((x) => withindaterange(timeofaccess, x)) } false }
but getting error while doing filter , stream. can 1 point direct direction ? references ? getting error on getvalidusages :
compile error “cannot resolve reference project such signature
this difficult answer since unfamiliar of types use. if guess there types following:
trait window { def getstarttime: localdate def getendtime: localdate } trait testwindows extends window { def gettv: list[window] def getcableuse: list[window] def getwifi: list[window] def getusageids: map[string, list[window]] }
then this:
def withindaterange(tocheck: localdate)(window: window): boolean = window.getstarttime.isbefore(tocheck) && window.getendtime.isafter(tocheck) // nothing should ever null in scala. if it's possible don't have consumptionwindows should either // model empty list or option[list[consumptionwindow]] def existseligibletestwindow(windows: list[window], timeofaccess: localdate): boolean = windows.exists(withindaterange(timeofaccess)) def getvalidusages(testwindows: testwindows, timeofaccess: localdate): set[string] = { val istv = existseligibletestwindow(testwindows.gettv, timeofaccess) val iscableuse = existseligibletestwindow(testwindows.getcableuse, timeofaccess) val iswifi = existseligibletestwindow(testwindows.getwifi, timeofaccess) val tvormovie: option[string] = if (istv) some("tv") else if (iscableuse || iswifi) some("movie") else none val byusageid = testwindows.getusageids.collect { case (key, windows) if existseligibletestwindow(windows, timeofaccess) => key }.toset tvormovie.toset ++ byusageid }
in original code there presumably items
value, in above assume testwindows testwindows = items.get(itemid).gettestwindows()
outside getvalidusages
function.
my example doesn't use java structures @ , uses scala core collections. other main difference use immutable data structures is, think, little easier follow , safer.
some items of note: 1) option.toset operation result in empty set when called upon none. 2) there example of function currying used withindaterange
method. 3) have no idea original types , had guess @ relevant parts.
Comments
Post a Comment