This content has been marked as final. Show 4 replies
How to handle such code?How to answer such a meaningless question?
How would you declare a comparator?
SortedSet<? extends Integer> set = …; Comparator</*what should stay here*/> c = set.compartor();
You can't reasonably expect to write a Comparator for the elements of such a set, because you don't know the type of the elements. That's what the two question-marks are really telling you -- it doesn't make sense to do that.
Integer is a final class, it can not be extended. So, you could as well write:
On the other hand, If you were thinking of a set allowing different kinds of numbers, this is what you might be looking for.
SortedSet<Integer> set = new TreeSet<Integer>(); Comparator<? super Integer> c = set.comparator(); c.compare(new Integer(5), new Integer(5));
Why it must be "super" instead of "extends" is explained by [PECS rule|http://stackoverflow.com/questions/2723397/java-generics-what-is-pecs].
SortedSet<? super Number> set = new TreeSet<Number>(); Comparator<? super Number> c = set.comparator(); c.compare(new Integer(5), new Long(5));