4 Replies Latest reply on Dec 10, 2012 2:39 PM by 969557

# How to handle such code: ? super ? extends Type

Hi I faced of a problem which I don't handle:
``````SortedSet<? extends Integer> set = new TreeSet<>();
...
Comparator<? super ? extends Integer> c = set.comparator();//how to handle such code?
...``````
Best Regards

Andrej
• ###### 1. Re: How to handle such code: ? super ? extends Type
How to handle such code?
How to answer such a meaningless question?
• ###### 2. Re: How to handle such code: ? super ? extends Type
How would you declare a comparator?
``````SortedSet<? extends Integer> set = …;
Comparator</*what should stay here*/> c = set.compartor(); ``````
• ###### 3. Re: How to handle such code: ? super ? extends Type
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.
• ###### 4. Re: How to handle such code: ? super ? extends Type
Integer is a final class, it can not be extended. So, you could as well write:
``````SortedSet<Integer> set = new TreeSet<Integer>();
Comparator<? super Integer> c = set.comparator();
c.compare(new Integer(5), new Integer(5));``````
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<? super Number> set = new TreeSet<Number>();
Comparator<? super Number> c = set.comparator();
c.compare(new Integer(5), new Long(5));``````
Why it must be "super" instead of "extends" is explained by [PECS rule|http://stackoverflow.com/questions/2723397/java-generics-what-is-pecs].