Results 1 to 3 of 3
  1. #1
    gunitinug is offline Member
    Join Date
    Dec 2016
    Posts
    13
    Rep Power
    0

    Default Collections class

    In Collections class max() method has signature
    Java Code:
    static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp)
    In the arguments coll and comp, why coll has <? extends T> and comp has <? super T>? I know what these means but not sure why they are chosen.

    Been googling and I got:

    In nutshell easy to remember PECS

    Use the <? extends T> wildcard if you need to retrieve object of type T from a collection.
    Use the <? super T> wildcard if you need to put objects of type T in a collection.
    If you need to satisfy both things, well, don’t use any wildcard. As simple as it is.

    But I still don't get the max() signature.

  2. #2
    angstrem is offline Member
    Join Date
    Mar 2013
    Posts
    13
    Rep Power
    0

    Default Re: Collections class

    Use the <? extends T> wildcard if you need to retrieve object of type T from a collection.
    Use that if you return T from a method.

    Use the <? super T> wildcard if you need to put objects of type T in a collection.
    Use that if you need to pass T as an argument to a method.

    Max looks for the maximum of a collection. To do this, it first needs to get elements from the collection (return), then feed them to the comparator (as arguments). Hence the requirements to the two objects.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Collections class

    A useful exercise is to have a super class of fruit and to have apple and pear subclass it. Use an integer for the weight of each
    and then write your own max routine. Change around the wildcards or omit them and see under what inheritance hierarchies things
    either work or don't work. The Collections signature is to allow the most general case possible, which is usually always the goal.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Collections
    By tfunk92 in forum New To Java
    Replies: 2
    Last Post: 10-29-2013, 02:49 PM
  2. Collections sorting a list made of generic class objects
    By andreiutz10 in forum New To Java
    Replies: 5
    Last Post: 02-07-2012, 05:56 PM
  3. Collections
    By NatureFreshMilk in forum New To Java
    Replies: 12
    Last Post: 05-23-2011, 08:12 AM
  4. Collections, Collection From one class to another
    By D.Calladine in forum New To Java
    Replies: 2
    Last Post: 11-23-2010, 02:10 PM
  5. Collections, Collection From one class to another
    By D.Calladine in forum New To Java
    Replies: 2
    Last Post: 11-16-2010, 03:39 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
  •