Results 1 to 3 of 3
  1. #1
    yma16 is offline Member
    Join Date
    Apr 2011
    Posts
    52
    Rep Power
    0

    Default Question about Interface Collection<E>

    In Interface Collection<E>, it has

    addAll(Collection<? extends E> c)

    removeAll(Collection<?> c)

    Why does it use <? extends E> and <?> in these two method? Why not use

    removeAll(Collection<? extends E> c) ?

    Thank you.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Have you tried reading what the two things do and how they differ? Or if they differ? If not, here is a link to where you should be able to find your answer:
    Lesson: Generics (The Java™ Tutorials > Bonus)

    The ? is a wildcard, and <? extends X> is a bounded wildcard to help you narrow your search. It has something to do with reading and writing.

    For example, with the removeAll method, it really doesn't care what the type of the passed in argument is. However, when adding a collection you only want types of E and it's subclasses to be added to the collection. You don't want to allow a collection of type Ham to be added to a collection of Type Hamster, that wouldn't make much sense.

    My understanding of generics is admittedly rusty so my explanation may have some flaws. That is where the tutorials will come in handy. Check them out.
    Last edited by sunde887; 04-11-2011 at 03:14 AM.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Why not use removeAll(Collection<? extends E> c) ?

    I guess the reason is the same the reason why remove() is declared as "boolean remove(Object)" while add() is declared as "boolean add(E)".

    There's discussion at stackoverflow.

Similar Threads

  1. Question concerning interface functionality
    By Morning-owl in forum New To Java
    Replies: 3
    Last Post: 04-04-2011, 09:25 PM
  2. Interface Question
    By superman1938 in forum Advanced Java
    Replies: 1
    Last Post: 12-14-2010, 05:28 AM
  3. Garbage Collection Question
    By DrMath in forum New To Java
    Replies: 5
    Last Post: 10-03-2009, 11:08 AM
  4. question on listener interface
    By Minu in forum Java Servlet
    Replies: 1
    Last Post: 01-16-2009, 11:33 AM
  5. Interface question in java
    By tony404 in forum Advanced Java
    Replies: 2
    Last Post: 06-27-2008, 12:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •