lambda - how to operate two adjacent objects in java8 function style,change below codes -
the latter staffoperinfo object need compare former one,how change below code java8 style?my english poor,i hope not mind, thanks!
staffoperinfo before = null; (staffoperinfo : staffmap.get(staffid)) { if (before == null) { before = so; } else { if (so.gettermip()!=null&&!so.gettermip().equals(before.gettermip()) && so.getacceptdate().gettime()- before.getacceptdate().gettime()<step*1000) { if (filtermap.containskey(staffid)) { filtermap.get(staffid).add(before); filtermap.get(staffid).add(so); } else { list<staffoperinfo> ll = new arraylist<>(); ll.add(before); ll.add(so); filtermap.put(staffid, ll); } } before = so; } }
streams won't make code better. said below 1 of way of doing if collection list or array(if not, wont work):
list<staffoperinfo> list = staffmap.get(staffid); intstream.range(1, list.size() - 1) // stream on indices of list .maptoobj(i -> new staffoperinfo[]{list.get(i - 1), list.get(i)}) // pair of staffoperinfo .filter(arr -> !objects.equals(arr[0].gettermip(), arr[1].gettermip()) // filter pairs based on if condition && (arr[1].getacceptdate().gettime() - arr[0].getacceptdate().gettime() < step * 1000)) .foreach(arr -> { if (filtermap.containskey(staffid)) filtermap.get(staffid).addall(arrays.aslist(arr)); else filtermap.put(staffid, arrays.aslist(arr)); });
Comments
Post a Comment