How to get inverse of a comparator in java

前端 未结 4 1522
执念已碎
执念已碎 2021-01-18 01:07

In a method I receive a generic object E extends Comparable as an argument. Now i want to create two priority queues.One which uses the comparato

4条回答
  •  生来不讨喜
    2021-01-18 01:34

    Your object E extends java.lang.Comparable, but it is not a java.util.Comparator.

    Create your first queue w/o a Comparator and you'll get the ordering in your compareTo function, then create a java.util.Comparator that does the comparison in reverse (just call a.compareTo(b) and then negate the result) and create your second queue with that comparator.

提交回复
热议问题