Reputation: 597
I have written a following code in imperative style that is working fine . but i want to convert it to java 8 , I have tried it but could not able to get in most elegant way .
List<Wrapper> futureList = new ArrayList<>();
List<Wrapper> pastList = new ArrayList<>();
List<Wrapper> list = fooRepository.findAll();
for(Wrapper data : list){
if(data.getSchedule().toInstant().isAfter(new Date().toInstant())
futureList.add(data);
else
pastList.add(data);
}
Upvotes: 1
Views: 44
Reputation: 4496
Well, the easiest thing to do if you want to use Stream
s is using Collectors.partitioningBy
like that:
Map<Boolean, List<Wrapper>> map = list.stream()
.collect(Collectors.partitioningBy(data -> data.getSchedule().toInstant().isAfter(Instant.now())));
List<Wrapper> pastList = map.get(false);
List<Wrapper> futureList = map.get(true);
Upvotes: 3