Why does Java's TreeSet not specify that its type parameter must extend Comparable?
Posted
by Tarski
on Stack Overflow
See other posts from Stack Overflow
or by Tarski
Published on 2010-04-13T19:03:19Z
Indexed on
2010/04/13
19:13 UTC
Read the original article
Hit count: 328
e.g. The code below throws a ClassCastException when the second Object is added to the TreeSet. Couldn't TreeSet have been written so that the type parameter can only be a Comparable type? i.e. TreeSet would not compile because Object is not Comparable. That way generics actually do their job - of being typesafe.
import java.util.TreeSet;
public class TreeSetTest {
public static void main(String [] args) {
TreeSet<Object> t = new TreeSet<Object>();
t.add(new Object());
t.add(new Object());
}
}
© Stack Overflow or respective owner