• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Gnerics Wild Card

 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All
Please explain following lines from Generics Tutorial (Article 9)

A Comparator objcet can be passed to the constructor of TreeSet which will be used to sort elements of the TreeSet according to desired ordering.



The Comparator interface is essentially :

Suppose we want to create a TreeSet<String> and pass in a suitable comparator, We need to pass ut a Comparator that can compare String. This can be done by a Comparator<String>, but a Comparator<Object> will do just well. However, we won't be able to invoke constructor given above on a Comaparator<Object>. We can use a lower bounded wildcard to get flexibility we want:



Any example will be great

Thanks
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically what they are saying is that making E the type accepted by the Comparator limits the Comparator to only accept E.

By using E as the lower bound, then you can send a Comparator that has a parameterized type that is any superclass of E.
[ May 28, 2006: Message edited by: Keith Lynn ]
 
Amirr Rafique
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Keith Lynn:
Basically what they are saying is that making E the type accepted by the Comparator limits the Comparator to only accept E.

By using E as the lower bound, then you can send a Comparator that has a parameterized type that is any superclass of E.

[ May 28, 2006: Message edited by: Keith Lynn ]



Comparator<String> will only work with String. and If we say Comparator<Object> it will work only with object i t can't work for String or any other type.

if we say Comparator<? super E> then it can work with any super type of E.
This is what i understand but I am confused in what benefit we will get
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The benefit is that you have one method which can accept either a Comparator<String> or Comparator<Object>
 
Men call me Jim. Women look past me to this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic