Java 静态比较器集合。反转顺序(Comarator)显示警告

Java 静态比较器集合。反转顺序(Comarator)显示警告,java,Java,无论是否使用强制转换,我都会收到相同的警告,请帮助我删除警告。问题不是来自强制转换,而是来自比较器的通用参数 Note: 5.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 取代 //This one is not parameterized Comparator fs=(Comparator<Integer>)Collections.rever

无论是否使用强制转换,我都会收到相同的警告,请帮助我删除警告。

问题不是来自强制转换,而是来自比较器的通用参数

Note: 5.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
取代

//This one is not parameterized
Comparator fs=(Comparator<Integer>)Collections.reverseOrder(rs); // Same result with casting or without casting
Collections.sort(ts,fs)

问题不在于强制转换,而是将其分配给原始比较器引用。将类型参数放置在fs引用上:

Comparator<Integer> fs=(Comparator<Integer>)Collections.reverseOrder(rs); 
强制转换是不必要的,可以删除。

使用Comparator fs=Collections.reverseOrderrs

Comparator<Integer> fs=(Comparator<Integer>)Collections.reverseOrder(rs); 
Comparator<Integer> fs = Collections.reverseOrder(rs);