0

I am having hard time understanding the best way to use generic type parameters.

Consider a priority queue implementation that implements an interface and requires all elements to be comparable of its type. The class has a constructor of with input type collection which provides initial elements. Here are a few alternatives. Can you explain differences? And provide the most general way of such declaration?

public class HeapMinPQ<E extends Comparable<E>> implements MinPQ<E> {
     ...
     public HeapMinPQ( Collection<? extends Comparable<E>> source) {
     ...
     }
}


public class HeapMinPQ<E extends Comparable<E>> implements MinPQ<E> {
     ...
     public HeapMinPQ( Collection<? extends E> source) {
     ...
     }
}


public class HeapMinPQ<E extends Comparable<? super E>> implements MinPQ<E> {
     ...
     public HeapMinPQ( Collection<? extends Comparable<E>> source) {
     ...
     }
}
2
  • I am not offended. It is not a homework. I am studying for an interview. My attempt to implement heap based priority queue. Commented Jun 28, 2016 at 0:08
  • relevant: stackoverflow.com/questions/2723397/… & the reason why the most generic type is E extends Comparable<? super E>. Commented Jun 28, 2016 at 0:45

1 Answer 1

1

Your constructors should not use Collection<? extends E> but Collection<E> as the type of the parameter. Also, your examples are incomplete; they don't show how the arguments are intended to be used.

In the first case, source contains elements guaranteed to be of type capture ? of Comparable<E>. The base type might or might not be compatible with E. So it wouldn't necessarily be safe to extract an element from source and use it against an E instance.

In the second, source contains elements guaranteed to be of type capture ? of E, which means the base type is compatible with E. It would be usable against an instance of E as long as the particular subtype doesn't matter.

In the third, the class time is weird. It says that E is comparable to its supertypes, which is not generally true for comparable types. It is likely to cause you heartache, and also there's no guarantee that the capture ? of Comparable<E> base type of source will work to compare to E or be otherwise compatible with E.

Generally you don't want to mess with captures of E in a type generic over E, but the full details are complex. I suggest Angelika Langer's excellent site, http://www.angelikalanger.com/, particularly http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html, to obtain more knowledge, as well as the articles on Developerworks and in the Oracle Tutorials.

Sign up to request clarification or add additional context in comments.

1 Comment

One more detail please; does the second case guarantee that all collection elements are comparable with each other? Since I declared E to be that way in class declaration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.