• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

TreeSet Question

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

K&B book, Page: 563. Chapter: 7. Topic: Collections

The issue is that whenever you want a collection to be sorted, its elements must
be mutually comparable. Remember that unless otherwise specified, objects of
different types are not mutually comparable.



How can we specify objects of different types to be mutually comparable?
If a Cat class implements Comparable<Dog>, and
a Dog class implements Comparable<Cat>
Can we say now that Dog and Cat are mutually comparable?
I have tried the above but its not working and I am getting ClassCastException.
Please explain!

Thanks
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Could you please post your code in which the ClassCastException occurs.
That might help to identify the bug.
 
Ansar Shah
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the code:


I tried to override equals method for Dog and Cat, but in vain. Here is the equals method's Dog version:



There is no exception when first two objects are added to the set. Its only when I try to add a third object.

Thanks
 
Vinayagar Karpagam
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The ClassCastException is being thrown only for the second Dog object. If your try to add another Cat Object also, the same exception will be thrown. Because, when we observe the sequence,

1) empty set
2) first Cat is added; This is assumed to be the smallest in sorting order.
3) then Dog is added; which can compare the already existing Cat object in the set since it has signature compare(Cat c). This compares it and places it appropriately. In our case, assume it is larger and kept second in sorting order.
4) then second Dog is added. This again can compare itself with Cat. So, it will proceed with comparison with the Dog object already in set. Here since it has only compare(Cat), this expects the other object to be a Cat but it is a Dog. Thats why the exception occurs.

So, you cannot have a Dog class that compares itself with a Cat class.

But still you can achieve this (not recommended though), using no generic types. Keep simple Comparable. Use instanceof inside the compare() method.
 
Ansar Shah
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Vinayagar! I got it
 
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic