How-to chain and apply a stream of comparators?

霸气de小男生 提交于 2019-12-21 04:06:07

问题


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 way than the following code to achive this?

Stream unsorted = ...;
Stream<Comparator> comparators = ...;

Comparator compareFunc = comparators.reduce(null, (a, b) -> {
        if(a == null) {
            return b;
        }else {
            return  a.thenComparing(b); 
        }

    });

Stream result = unsorted.sorted(compareFunc);

回答1:


Don’t use an identity value for Comparators. If the comparators stream is empty (i.e. does not contain any Comparator) you should not sort:

Stream result=comparators.reduce(Comparator::thenComparing)
             .map(unsorted::sorted).orElse(unsorted);

Note that if the comparators stream contains only a single Comparator, that Comparator will be the result of the reduction.


The method reference passed to Optional.map might need some experience to get used to it. So it might be worth using the more verbose lambda syntax to show what’s going on there:

Stream<String> result=comparators.reduce(Comparator::thenComparing)
    .map((comparator) -> unsorted.sorted(comparator)).orElse(unsorted);

That’s a matter of programming style or personal preference and might change over time.




回答2:


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<Comparator> compareFunc = comparators.reduce(Comparator::thenComparing);
Stream<?> result = compareFunc.isPresent() ? result.sorted(compareFunc.get())
                                           : unsorted;


来源:https://stackoverflow.com/questions/25850008/how-to-chain-and-apply-a-stream-of-comparators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!