Unchecked call to compareTo
Posted
by
Dave Jarvis
on Stack Overflow
See other posts from Stack Overflow
or by Dave Jarvis
Published on 2011-01-02T05:31:03Z
Indexed on
2011/01/02
5:54 UTC
Read the original article
Hit count: 303
Background
Create a Map
that can be sorted by value.
Problem
The code executes as expected, but does not compile cleanly:
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.
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
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
)?
Related
- TreeMap sort by value
- How to sort a Map on the values in Java?
- http://paaloliver.wordpress.com/2006/01/24/sorting-maps-in-java/
Thank you!
© Stack Overflow or respective owner