• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

New to Generics

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all I am new to Generics but I want to try it out building a class that uses generics. I am stuck at something so I hope someone would give me a hand. Thank you.

Basically, I want the supported type to be a solid class of interface java.lang.Comparable, so I thought may be this would work:



Then later on, in one of my defined methods, I want the argument to run a method which is defined in the Comparable:



Eclipse complains about the type is not applicable for the arguments (E)

I wonder what is wrong here.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You know perfectly well what type is going to be passed to the Comparable. So don't put ? there. This is what you want:
 
S Chan
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see. That solves the problem. Thank you for your help!

Actually before I had ? , I was trying with ? extends E and ? super E .

I had read the documents but didn't quite understand what they are and when to use them.

Can someone be kind and explains them to me in plain English?
 
S Chan
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I find it very confusing because I don't know when to use ? and when to use E.

Let's take an example, I have the following:



I know I shouldn't use new ArrayList<?>() here but how can I make sure clones is a list of ? type (defined by the argument)?

Also, even thought I have for the class declaration, why can't I have in the containsAll method?
 
S Chan
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would this be a better way of re-writing containsAll?




Need some expert advice here. I am so bad at this... sigh...
 
Marshal
Posts: 79174
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since the E class might not implement Comparable, but might extend a class which implements Comparable, the actual type parameter should be
<E extends Comparable<? super E>>
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell is right. Without the ? super part you wouldn't be able to use java.sql.Timestamp for instance, as it extends java.util.Date and therefore implements Comparable<java.util.Date> - not Comparable<java.sql.Timestamp>.
 
S Chan
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah! That rings the bell!

Thank you for the explanations and advices!
 
Campbell Ritchie
Marshal
Posts: 79174
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done ... and you're welcome.
 
reply
    Bookmark Topic Watch Topic
  • New Topic