简体   繁体   English

未选中的compareTo调用

[英]Unchecked call to compareTo

Background 背景

Create a Map that can be sorted by value. 创建一个可以按值排序的Map

Problem 问题

The code executes as expected, but does not compile cleanly: 该代码将按预期执行,但不能完全编译:

http://pastebin.com/bWhbHQmT http://pastebin.com/bWhbHQmT

public class SortableValueMap<K, V> extends LinkedHashMap<K, V> {
  ...
  public void sortByValue() {
      ...
      Collections.sort( list, new Comparator<Map.Entry>() {
          public int compare( Map.Entry entry1, Map.Entry entry2 ) {
            return ((Comparable)entry1.getValue()).compareTo( entry2.getValue() );
          }
      });
  ...

The syntax for passing Comparable as a generic parameter along to the Map.Entry<K, V> (where V must be Comparable ?) -- so that the (Comparable) typecast shown in the warning can be dropped -- eludes me. 用于将Comparable作为通用参数传递给Map.Entry<K, V>的语法(其中V必须是Comparable吗?)-以便可以删除警告中显示的(Comparable)类型转换-使我难以理解。

Warning 警告

Compiler's cantankerous complaint: 编译器的怪异抱怨:

SortableValueMap.java:24: warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type java.lang.Comparable SortableValueMap.java:24:警告:[unchecked]对未经检查的对compareTo(T)的调用,作为原始类型java.lang.Comparable的成员

  return ((Comparable)entry1.getValue()).compareTo( entry2.getValue() ); 

Question

How can the code be changed to compile without any warnings (without suppressing them while compiling with -Xlint:unchecked )? 如何将代码更改为在没有任何警告的情况下进行编译(在使用-Xlint:unchecked进行编译时不抑制它们)?

Related 有关

Thank you! 谢谢!

Declare the V type to extend the Comparable<V> interface. 声明V类型以扩展Comparable<V>接口。 That way, you can remove the cast of the Map.Entry objects down to (Comparable) and use the inferred type instead: 这样,您可以将Map.Entry对象的类型转换删除为(Comparable)并改用推断的类型:

public class SortableValueMap<K, V extends Comparable<V>>
             extends LinkedHashMap<K, V> {

.... ....

    Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
        public int compare(Map.Entry<K, V> entry1, Map.Entry<K, V> entry2) {
            return entry1.getValue().compareTo(entry2.getValue());
        }
    });

The value should be a subclass of comparable. 该值应该是可比较的子类。

SortableValueMap<K, V extends Comparable>

Try the above. 尝试以上。

The syntax for passing Comparable as a generic parameter along to the Map.Entry (where V must be Comparable?) -- so that the (Comparable) typecast shown in the warning can be dropped -- eludes me. 将Comparable作为通用参数传递给Map.Entry的语法(其中V必须是Comparable?),以便可以删除警告中显示的(Comparable)类型转换,这使我感到困惑。

How about: 怎么样:

public class SortableValueMap <K, V extends Comparable<V>> extends LinkedHashMap<K, V> { 
  ...
    Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
        public int compare(Map.Entry<K, V> entry1, Map.Entry<K, V> entry2) {
            return (entry1.getValue()).compareTo(entry2.getValue());
        }
    });

but this may be better, depending on your intent: 但这可能会更好,具体取决于您的意图:

public class SortableValueMap <K, V extends Comparable<? super V>> extends LinkedHashMap<K, V> { ...

See http://download.oracle.com/javase/tutorial/extra/generics/morefun.html 参见http://download.oracle.com/javase/tutorial/extra/generics/morefun.html

It isn't necessary that T be comparable to exactly itself. T不必与自己完全可比。 All that's required is that T be comparable to one of its supertypes. 所需要做的就是让T与其超类型之一可比。 This give us: 这给我们:

 public static <T extends Comparable<? super T>> max(Collection<T> coll) 

... This reasoning applies to almost any usage of Comparable that is intended to work for arbitrary types: You always want to use Comparable <? super T> ...这种推理几乎适用于可用于任意类型的Comparable的几乎所有用法:您始终想使用Comparable <? super T> Comparable <? super T> . Comparable <? super T> ... ...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM