• 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

Generic TreeSet

 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to write a generic method that will take any kind of record in a database and output it as a string to the standard output.

File 1: TreeSetTest.java


File 2: Record.java (I think that I need to implement a generic type for comparable so I don't get an "unchecked warning"


File 3: Item.java


file 4: Invoice.java


[ December 27, 2008: Message edited by: Kaydell Leavitt ]
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need declare the generic type E as a part of the class declaration or Method declaration:
public abstract class Record<E> implements Comparable<E> {

Modify the compareTo method of Record as follows:



Further you should override the toString() method in your Item and Invoice classes to return a meaningful representation of your object.
e.g. in your Item class



Hope this helps
 
Kaydell Leavitt
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your help. I think that I still need to do some more work because I'm getting a warning: "unchecked" in the cast to Record.


[ December 27, 2008: Message edited by: Kaydell Leavitt ]
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok. my previous post was a quick go through to avoid any compilation errors.
Since you don't even want any compiler warnings generated because of type unsafe code I suggest the following:

You would need to make your output() method as generic typed because you need a method which iterates over any collection of objects and print their String representation. So <T> implies any kind of object.


and make following changes to your Record class by making the generic type of Comparable interface as <Record> because the compareTo() uses the id field for comparison. Further the compareTo() method is inherited by all concrete subclasses of abstract class Record.



and



Hope this helps.
[ December 27, 2008: Message edited by: Harvinder Thakur ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic