I have a situation similar to the follows:
List<ObjectA> all = repository.findAll(Sort.by(Sort.Direction.ASC, "status"));
This find a list of object with three possible states: OPEN, CLOSED, PROGRAMMED
Now I must return a List that shows to the user the last PROGRAMMED object, the first OPEN object and all closed objects. I have writing something like that:
ObjectA programmed = all.stream().filter(isPROGRAMMED()).reduce((x,y) -> y).orElse(null);
ObjectA open = all.stream().filter(isOPEN()).findFirst().get();
List<ObjectA> closed = all.stream().filter(isCLOSED()).collect(Collectors.toList());
isOPEN(), isPROGRAMMED() and isCLOSED() are Predicate I have written as follows:
private static Predicate<ObjectA> isPROGRAMMED() {
return x -> x.getStatus().equals(Stato.PROGRAMMED);
}
private static Predicate<ObjectA> isOPEN() {
return x -> x.getStatus().equals(Stato.OPEN);
}
private static Predicate<ObjectA> isCLOSED() {
return x -> x.getStatus().equals(Stato.CLOSED);
}
Now I want just to merge the two object and the third list into one. List should contain programmed item (if present) open object (if present) and after that all closed items. I jave tried with Stream.of, Stream.concat but I always obtain some compilation errors..
Are there some way to do it with Lambdas and with few code?
List should contain- that doesn't require the usage of streams.list.add(0, open):thenlist.add(0, programmed):