Java 如何对使用番石榴范围值作为键的集合进行排序?

Java 如何对使用番石榴范围值作为键的集合进行排序?,java,sorting,guava,Java,Sorting,Guava,我有以下地图,Range是关键: | Range<Integer> | Set<Range<BigDecimal>> ------------------------------------------------------------ | 1..100 | [0.01..1.00], [2.01..15.00], [1.01..2.00] ---------------------------------------------------

我有以下地图,
Range
是关键:

| Range<Integer> | Set<Range<BigDecimal>>
------------------------------------------------------------
| 1..100         | [0.01..1.00], [2.01..15.00], [1.01..2.00]
------------------------------------------------------------
| 151..250       | [0.01..1.00], [2.01..15.00], [1.01..2.00]
------------------------------------------------------------
| 101..150       | [1.01..7.00], [0.01..1.00]
------------------------------------------------------------
|范围|设置
------------------------------------------------------------
| 1..100         | [0.01..1.00], [2.01..15.00], [1.01..2.00]
------------------------------------------------------------
| 151..250       | [0.01..1.00], [2.01..15.00], [1.01..2.00]
------------------------------------------------------------
| 101..150       | [1.01..7.00], [0.01..1.00]
------------------------------------------------------------
我还有以下比较程序实现:

public class RangeComparator<T extends Comparable<T>> implements Comparator<Range<T>> {

    @Override
    public int compare(Range<T> first, Range<T> second) {
        int comparatorResult = first.lowerEndpoint().compareTo(second.lowerEndpoint());
        return comparatorResult == 0 ? first.upperEndpoint().compareTo(second.upperEndpoint()) : comparatorResult;
    }
}
公共类RangeComparator实现Comparator{
@凌驾
公共整数比较(范围第一,范围第二){
int comparatorResult=first.lowerndpoint().compareTo(second.lowerndpoint());
返回comparatorResult==0?first.upperEndpoint().compareTo(second.upperEndpoint()):comparatorResult;
}
}
我想使用这个比较器对映射中的值进行排序,然后对映射本身进行排序,从而得到以下结果:

| Range<Integer> | Set<Range<BigDecimal>>
------------------------------------------------------------
| 1..100         | [0.01..1.00], [1.01..2.00], [2.01..15.00]
------------------------------------------------------------
| 101..150       | [0.01..1.00], [1.01..7.00]
------------------------------------------------------------
| 151..250       | [0.01..1.00], [1.01..2.00], [2.01..15.00]
------------------------------------------------------------
|范围|设置
------------------------------------------------------------
| 1..100         | [0.01..1.00], [1.01..2.00], [2.01..15.00]
------------------------------------------------------------
| 101..150       | [0.01..1.00], [1.01..7.00]
------------------------------------------------------------
| 151..250       | [0.01..1.00], [1.01..2.00], [2.01..15.00]
------------------------------------------------------------
Guava Range类是最终类,不实现Compariable

我可以在不手动执行排序的情况下对此进行排序吗?如果是,如何使用and的构造函数,并将比较器作为参数,例如


公共树映射(Comparator
TreeSet
TreeSet
的构造函数采用显式的
Comparator
。是什么阻止了您使用它?为什么您不能使用TreeSet或TreeSet?@如果您的答案正确,请再次发布。使用默认构造函数的无关TreeSet供应商抛出了ClassCastException,并且我将其归因于我尝试进行的地图排序。自定义比较器工作正常。
public TreeMap(Comparator<? super K> comparator)