Java keyword "super"
Joseph Sinclair
plug-discussion at stcaz.net
Wed May 25 14:55:29 MST 2016
In the generic specifier you mentioned (Comparator<? super E> comp) super is telling the compiler (all generics are erased during compilation) to typecheck the parameters to the method and ensure that the objects compared by the Comparator are supertypes of the generic type parameter "E".
It's somewhat odd usage (the designer is requesting a Comparator of unknown objects that are supertypes of E. Basically, you can compare objects of type E, or any supertype, including "Object", but probably only the specific supertype, so it's probable objects of type "E" are not properly compared by the Comparator).
Typically you'd want to ensure that a Comparator can handle the objects you're working with and any subtype (Comparator<? extends E>), what this code does is request that the parameter can have just about anything in it, as long as an object of type "E" can be upcast to it.
I'd have to see the method to figure out what it's doing, but my first guess would be that the code is either incorrect (the designer meant to use "extends") or doing something very unusual, and probably unstable.
As far as collections, it's almost never done because a simple SomeCollection<?> is usually equivalent. SomeCollection<? super T> would slightly restrict the members of the collection to be supertypes of the generic parameter <T>. Objects of type "T" or any of it's supertypes (most often, this is just "Object" due to Java's shallow type hierarchies) could be inserted into the collection, but the consumer of the collection would have difficulty using the objects, as each object could be T or any of the supertypes, including "Object" which is literally anything.
I've seen this used in legacy compatibility code, but it's always been marked as legacy conversion with the clear documented intent to remove it as soon as the old legacy cruft can be modified to use the proper generic functions and accept typed collections.
To clarify, this usage returns a collection of "Object" to older code, but internally ensures that all members are, in fact, type "T", never any supertype. This allows the old code (which expects Collection, or Collection<?>) to compile without errors until it's modified to accept a Collection<T> or Collection<? extends T>.
On 05/21/2016 06:47 PM, trent shipley wrote:
> "extends" used with a wildcard {"?"} such as SomeCollection<? extends T>
> makes sense to me. After reading stackoverflow, I am only somewhat more
> enlightened on what SomeCollection<? super T> means, how it is used, and
> why one might want to use it.
>
> Trent.
>
> On Sat, May 21, 2016 at 6:18 PM James Mcphee <jmcphe at gmail.com> wrote:
>
>> Check the answer to this stackoverflow. It says it better than I can.
>> http://stackoverflow.com/questions/3009745/what-does-the-question-mark-in-java-generics-type-parameter-mean
>>
>> On Sat, May 21, 2016 at 1:08 PM, trent shipley <trent.shipley at gmail.com>
>> wrote:
>>
>>> Can anyone tell me what the keyword "super" does in this parameter list
>>> for the constructor below? I can only find documentation for the more
>>> mundane use when a method references an ancestor.
>>>
>>> public WeightedCollection(Comparator<? super E> comp)
>>>
>>>
>>> package pcgen.base.util;
>>>
>>> <</ deleted stuff>>
>>>
>>> /**
>>>
>>> * Constructs an empty WeightedCollection with the given Comparator used to
>>>
>>> * establish equality and order in the WeightedCollection.
>>>
>>> *
>>>
>>> * @param comp
>>>
>>> * The Comparator this Set will use to determine equality and
>>>
>>> * order of the WeightedCollection
>>>
>>> */
>>>
>>> public WeightedCollection(Comparator<? super E> comp)
>>>
>>> {
>>>
>>> if (comp == null)
>>>
>>> {
>>>
>>> theData = new ListSet<WeightedItem<E>>();
>>>
>>> }
>>>
>>> else
>>>
>>> {
>>>
>>> theData = new TreeSet<WeightedItem<E>>(
>>>
>>> new WeightedItemComparator<E>(comp));
>>>
>>> }
>>>
>>> }
>>>
>>>
>>>
>>> ---------------------------------------------------
>>> PLUG-discuss mailing list - PLUG-discuss at lists.phxlinux.org
>>> To subscribe, unsubscribe, or to change your mail settings:
>>> http://lists.phxlinux.org/mailman/listinfo/plug-discuss
>>>
>>
>>
>>
>> --
>> James McPhee
>> jmcphe at gmail.com
>> ---------------------------------------------------
>> PLUG-discuss mailing list - PLUG-discuss at lists.phxlinux.org
>> To subscribe, unsubscribe, or to change your mail settings:
>> http://lists.phxlinux.org/mailman/listinfo/plug-discuss
>
>
>
> ---------------------------------------------------
> PLUG-discuss mailing list - PLUG-discuss at lists.phxlinux.org
> To subscribe, unsubscribe, or to change your mail settings:
> http://lists.phxlinux.org/mailman/listinfo/plug-discuss
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: OpenPGP digital signature
URL: <http://lists.phxlinux.org/pipermail/plug-discuss/attachments/20160525/eae04976/attachment.pgp>
More information about the PLUG-discuss
mailing list