java - 泛型、比较器和 Map 排序时出现问题

标签 java generics comparator

最初的目的是检索 HashMap 中按值排序的项目列表。

粗略的代码(名称只是简化):

public abstract class Thing<T> implements Iface<T> {
    private HashMap<T, Integer> map;

    static class DescendingValueComparator<K, V extends Comparable<V>> implements Comparator<Map.Entry<K, V>> {
        public int compare(Map.Entry<K,V> a, Map.Entry<K,V> b) {
            return (b.getValue().compareTo(a.getValue()));
        }
    }

    public LinkedHashMap<T, Integer> getSorted() {
        LinkedHashMap<T, Integer> linked = new LinkedHashMap<T, Integer>();
        ArrayList<Map.Entry<T, Integer>> s = new ArrayList<Map.Entry<T, Integer>>(map.entrySet());

        Arrays.sort(s, new DescendingValueComparator<T, Integer>());
        //...
        }
}

编译器错误:

Thing.java:30: error: no suitable method found for sort(ArrayList<Entry<T#1,Integer>>,DescendingValueComparator<T#1,Integer>)
        Arrays.sort(sorted, new DescendingValueComparator<T, Integer>());
              ^
    method Arrays.<T#2>sort(T#2[],int,int,Comparator<? super T#2>) is not applicable
      (cannot instantiate from arguments because actual and formal argument lists differ in length)
    method Arrays.<T#3>sort(T#3[],Comparator<? super T#3>) is not applicable
      (no instance(s) of type variable(s) T#3 exist so that argument type ArrayList<Entry<T#1,Integer>> conforms to formal parameter type T#3[])
    method Arrays.sort(Object[],int,int) is not applicable
      (actual and formal argument lists differ in length)
    method Arrays.sort(Object[]) is not applicable
      (actual and formal argument lists differ in length)
    method Arrays.sort(double[],int,int) is not applicable
      (actual and formal argument lists differ in length)
    method Arrays.sort(double[]) is not applicable
      (actual and formal argument lists differ in length)
    method Arrays.sort(float[],int,int) is not applicable
      (actual and formal argument lists differ in length)
    method Arrays.sort(float[]) is not applicable
      (actual and formal argument lists differ in length)
    method Arrays.sort(byte[],int,int) is not applicable
      (actual and formal argument lists differ in length)
    method Arrays.sort(byte[]) is not applicable
      (actual and formal argument lists differ in length)
    method Arrays.sort(char[],int,int) is not applicable
      (actual and formal argument lists differ in length)
    method Arrays.sort(char[]) is not applicable
      (actual and formal argument lists differ in length)
    method Arrays.sort(short[],int,int) is not applicable
      (actual and formal argument lists differ in length)
    method Arrays.sort(short[]) is not applicable
      (actual and formal argument lists differ in length)
    method Arrays.sort(long[],int,int) is not applicable
      (actual and formal argument lists differ in length)
    method Arrays.sort(long[]) is not applicable
      (actual and formal argument lists differ in length)
    method Arrays.sort(int[],int,int) is not applicable
      (actual and formal argument lists differ in length)
    method Arrays.sort(int[]) is not applicable
      (actual and formal argument lists differ in length)
  where T#1,T#2,T#3 are type-variables:
    T#1 extends Object declared in class Thing
    T#2 extends Object declared in method <T#2>sort(T#2[],int,int,Comparator<? super T#2>)
    T#3 extends Object declared in method <T#3>sort(T#3[],Comparator<? super T#3>)
1 error

我通常可以理解基本的泛型,但是我对 T#x 之类的东西有点困惑。我盲目地尝试改变一些看起来不对的事情,比如制作一个

new DescendingValueComparator<Map.Entry<T, Integer>>()

但是我已经盯着这个看了太久了,没有任何意义了。 (如果有人对泛型有很好的综合引用,我将不胜感激)。

谢谢

最佳答案

您正在 ArrayList 上使用 Arrays.sort

Arrays.sort 接受一个数组。

您应该使用Collections.sort

关于java - 泛型、比较器和 Map 排序时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26175524/

相关文章:

Scala 泛型 : How to declare that a type must be a case class?

Java 泛型 "upcast"到非参数化类型

java - 我如何通过 lambda 创建字符串比较器?

Java:关于 Collections ReverseComparator 的实现

java - 使用 IE8 时的 GWT 问题

ArrayList 中的 Java 排序对象

java - Android Studio 模拟器不显示测试 ADS

java - 作为递归方法参数的变量是如何处理的?

java - 在 Java 中使用泛型为类编写什么单元测试?

java - 按排名和字母顺序对 IRC 用户进行排序 - Java Comparator