I have a stream of unsorted items and a stream of comparators. I want to apply all the comparators onto the stream by using \"thenComparing\" (Multisort) Is there a more elegant
Using Mark Peters' suggestion and mixing it with a method reference, you could write your comparator this way:
Comparator compareFunc = comparators.reduce((a, b) -> 0, Comparator::thenComparing);
Or stealing a bit from Holger instead:
Optional compareFunc = comparators.reduce(Comparator::thenComparing);
Stream> result = compareFunc.isPresent() ? result.sorted(compareFunc.get())
: unsorted;