<E> and <? extends E>

?

-

I noticed some classes have "<E>" and "?". What does it mean?

e.g:

public class TreeSet<E>
extends AbstractSet<E>
implements SortedSet<E>, Cloneable, java.io.Serializable




public boolean addAll(Collection<? extends E> c)
 
T

Tor Iver Wilhelmsen

- said:
public class TreeSet<E>
extends AbstractSet<E>
implements SortedSet<E>, Cloneable, java.io.Serializable

Generic class declaration: The "parameter" E is replaced by the
programmer when using the class said:
public boolean addAll(Collection<? extends E> c)

Wildcard generic parameter: It means that the parameter can be any
generic Collection where the parametrized type extends the collection.

So if you have

class Foo { ...

class Fie extends Foo { ...

set = new TreeSet<Foo>();

otherCollection = new ArrayList<Fie>();

you can do a

set.addAll(otherCollection);

because the generic parameter for the second declaration is a class
that extends the parameter class of the first.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top