简体   繁体   English

Java Generics:compareTo和“capture#1-of?”

[英]Java Generics: compareTo and “capture#1-of ?”

The following gives me an error message: 以下给出了一条错误消息:

public static List<Comparable<?>> merge(Set<List<Comparable<?>>> lists) {
    List<Comparable<?>> result = new LinkedList<Comparable<?>>();
    HashBiMap<List<Comparable<?>>, Integer> location = HashBiMap.create();

    int totalSize;
    for (List<Comparable<?>> l : lists) {
        location.put(l, 0);
        totalSize += l.size();
    }

    boolean first;
    List<Comparable<?>> lowest; //the list with the lowest item to add
    int index;

    while (result.size() < totalSize) {
        first = true;

        for (List<Comparable<?>> l : lists) {
            if (! l.isEmpty()) {
                if (first) {
                    lowest = l;
                }
                else if (l.get(location.get(l)).compareTo(lowest.get(location.get(lowest))) <= 0) { //error here
                    lowest = l;
                }
            }
        }
        index = location.get(lowest);
        result.add(lowest.get(index));
        lowest.remove(index);
    }
    return result;
}

The error is: 错误是:

The method compareTo(capture#1-of ?) in the type Comparable<capture#1-of ?> is not applicable for the arguments (Comparable<capture#2-of ?>)

What's going on here? 这里发生了什么? I made the type of everything Comparable so I could call .compareTo and sort this list. 我做了所有Comparable的类型,所以我可以调用.compareTo并对此列表进行排序。 Am I using generics incorrectly? 我是否错误地使用泛型?

List<?> means "List of anything", so two objects with this type are not the same: One could be a list of String , the other a list of BigDecimal . List<?>表示“任何列表”,因此这种类型的两个对象不相同:一个可以是String列表,另一个可以是BigDecimal列表。 Obviously, those are not the same. 显然,那些不一样。

List<T> means "List of anything but when you see T again, it's the same T ". List<T>表示“除了再次看到T的所有内容列表,它都是相同的T ”。

You must tell the compiler when you mean the same type in different places. 当你在不同的地方使用相同的类型时,你必须告诉编译器。 Try: 尝试:

public static <T extends Comparable<? super T>> List<T> merge(Set<List<T>> lists) {
    List<T> result = new LinkedList<T>();
    HashBiMap<List<T>, Integer> location = HashBiMap.create();

[EDIT] So what does <T extends Comparable<? super T>> List<T> [编辑]那么<T extends Comparable<? super T>> List<T> <T extends Comparable<? super T>> List<T> mean? <T extends Comparable<? super T>> List<T>是什么意思? The first part defines a type T with the following properties: It must implement the interface Comparable<? super T> 第一部分定义了一个具有以下属性的类型T :它必须实现接口Comparable<? super T> Comparable<? super T> (or Comparable<X> where X is also defined in terms of T ). Comparable<? super T> (或Comparable<X> ,其中X也以T )。

? super T ? super T means that the type which the Comparable supports must T or one of its super types. ? super T表示Comparable支持的类型必须为T或其超类型之一。

Imagine for a moment this inheritance: Double extends Integer extends Number . 想象一下这个继承: Double extends Integer extends Number This is not correct in Java but imagine that Double is just an Integer plus a fraction part. 这在Java中是不正确的,但想象Double只是一个Integer加上一个小部分。 In this scenario, a Comparable which works for Number also works for Integer and Double since both derive from Number . 在这种情况下,适用于NumberComparable也适用于IntegerDouble因为它们都来自Number So Comparable<Number> would satisfy the super part for T being Number , Integer or Double . 因此, Comparable<Number>将满足TNumberIntegerDoublesuper部分。

As long as each of these types support the Comparable interface, they also satisfy the first part of the declaration. 只要这些类型中的每一个都支持Comparable接口,它们也满足声明的第一部分。 This means, you can pass in Number for T and the resulting code will also work when there are Integer and Double instances in the lists. 这意味着,您可以传入Number for T并且当列表中存在IntegerDouble实例时,生成的代码也将起作用。 If you Integer for T , you can still use Double but Number is not possible because it doesn't satisfy T extends Comparable anymore (the super part would still work, though). 如果你是T Integer ,你仍然可以使用Double但是Number不可能,因为它不再满足T extends Comparablesuper部分仍然可以工作)。

The next step is to understand that the expression between static and List just declares the properties of the type T which is used later in the code. 下一步是要理解staticList之间的表达式只声明了类型T的属性,后者在代码中使用。 This way, you don't have to repeat this long declaration over and over again. 这样,您就不必一遍又一遍地重复这个长的声明。 It's part of the behavior of the method (like public ) and not part of the actual code. 它是方法行为的一部分(如public ),而不是实际代码的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java generics - Comparable 类型中的方法 compareTo(capture#1-of?)<capture#1-of ?> 不适用于 arguments</capture#1-of> - java generics - The method compareTo(capture#1-of ?) in the type Comparable<capture#1-of ?> is not applicable for the arguments Java泛型编译错误“类 - Java generics compile error “Class<capture#1-of ? …” Java泛型:compareTo和“ capture#-of?” - Java Generics: compareTo and “capture#-of ?” Java泛型编译错误-方法method(Class <capture#1-of ? extends Interface> ) <type> 不适用于参数 - Java generics compilation error - The method method(Class<capture#1-of ? extends Interface>) in the type <type> is not applicable for the arguments Java Generics Comparable | 实现compareTo - Java Generics Comparable | implement compareTo Java泛型:Comparable类型的compareTo(capture#2-of?扩展E)方法不适用于自变量(Comparable) - Java Generics :The method compareTo(capture#2-of ? extends E) in the type Comparable is not applicable for the arguments (Comparable) 泛型:无法从 <capture#1-of ? extends Object,D> 至 <S,D> - Generics: cannot convert from <capture#1-of ? extends Object,D> to <S,D> Java泛型: <?> vs捕获 <?> - Java Generics: <?> vs capture<?> Java泛型捕获列表<?> - Java Generics Capture List<?> Java泛型“捕获?” - Java generics "capture of ?"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM