Results 1 to 4 of 4

Thread: generics

  1. #1
    arik23 is offline Member
    Join Date
    Apr 2011
    Rep Power

    Default generics

    I learn generic for the first time and I just get into wilcards.

    there is two declaration of method in the collection interface :
    1. boolean add(E e) // add the element e
    2. boolean removeAll(Collection<?> c) // remove the elements in c

    why the first one get a E param and in the second mehod is ? param
    I understand that when you use the ? mark you are saying that all the elements in the collection are from the same type but I think it was the same if you write in the second method : Collection<E> c.

    what am I missing?

    thank you

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    London, UK
    Blog Entries
    Rep Power


    the wildcard ? means its an unknown type.

    Read "? extends Animal" as "an unknown type that is a subtype of Animal,
    possibly Animal itself", which boils down to "some kind of animal".
    You can also specify an unknown type with an unbounded wildcard,
    which simply looks like <?>.
    An unbounded wildcard is essentially the same as saying <? extends Object>
    Wildcards (The Java™ Tutorials > Learning the Java Language > Generics)

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


    When adding an element to a collection you want to enforce the type to be the same as the collection it's being added to. You can't allow a ham to be added to a collection of hamsters, that wouldn't make sense.

    When removing from the collection you don't care what type you are looking to remove. Since removeAll returns a boolean it will remove the item if possible and return true. if it doesn't find the arguments it returns false. Perhaps it seems odd to allow someone to attempt to remove a collection of ham from a collection of hamsters.

    I believe removeAll removes items based on the objects equals method.

    Say you have a collection of objects and you remove a collection of oranges, it's possible to have oranges found in that collection of objects.

    Java Code:
    ArrayList<Object> objects = new ArrayList<Object>();\
    Orange o = new Orange();
    new ArrayList<Orange> oranges = new ArrayList<Orange>();
    Assuming orange has equals and hashcode implemented it will remove the everything in the oranges list from the objects list.

    For more, check out these links...
    api - Why aren&#39;t Java Collections remove methods generic? - Stack Overflow

  4. #4
    arik23 is offline Member
    Join Date
    Apr 2011
    Rep Power


    so, if I got it clear...

    if I write add method that look like this :
    add (<? extends E> e)
    each element in my collection could be from a different type (all of them extends E type (or from E itself)).

    now if I write remove method that looks like this :
    remove(Collection<?>) it won't be accept my collection because this is requires that all the elements in the given collection will be from the same type.

    I should have written instead : remove(Collection<? extends E>)
    Last edited by arik23; 04-19-2011 at 10:36 AM.

Similar Threads

  1. Need Help with Using Generics
    By badbeauty in forum Advanced Java
    Replies: 3
    Last Post: 04-09-2011, 08:07 AM
  2. Help with generics
    By shai in forum New To Java
    Replies: 0
    Last Post: 08-12-2010, 07:07 AM
  3. Generics
    By sakshamkum in forum Advanced Java
    Replies: 3
    Last Post: 06-03-2010, 10:12 PM
  4. generics
    By tascoa in forum Forum Lobby
    Replies: 2
    Last Post: 10-09-2008, 07:58 PM
  5. Help w/ generics
    By Hollywood in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 03:08 AM

Posting Permissions

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