<E> and <? extends E>

Discussion in 'Java' started by -, Apr 14, 2005.

  1. -

    - Guest

    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)
     
    -, Apr 14, 2005
    #1
    1. Advertising

  2. - <> writes:

    > 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, e.g. TreeSet<String>.

    > 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.
     
    Tor Iver Wilhelmsen, Apr 14, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon Skeet
    Replies:
    5
    Views:
    2,175
    Dale King
    Jul 8, 2003
  2. Jacky Luk
    Replies:
    4
    Views:
    870
    Bhupendra
    Nov 23, 2005
  3. Stacey
    Replies:
    0
    Views:
    2,175
    Stacey
    Jan 20, 2004
  4. Replies:
    8
    Views:
    545
  5. Alpha Blue
    Replies:
    0
    Views:
    108
    Alpha Blue
    Jan 30, 2010
Loading...

Share This Page