Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Set question

 
Arad Chear
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


since Double is-a Number
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Double is a Number. But the elements of a TreeSet have to be mutually comparable.
 
Arad Chear
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks so much lynn ,i got it
 
m ali
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Keith,

But we are declaring here the collection as type-safe number. Integer is a Number and Double is a Number? Why the above code fail to compile? please explain?

Thanks in advance.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


No, Ali, Did you compile the above code?
This code compiles fine but gives runtime exception because
of mutual incomparability between treeset objects. One object
is Integer and another is Double.

TreeSet uses Comparable interface's compareTo(...) method that
is implemented by the objects inserted into, tokeep the objects
in sorted manner. And an Integer and an Double can't be compared
because they are not mutually comparable as Keith said
already.




Regards,
cmbhatt
[ April 24, 2007: Message edited by: Chandra Bhatt ]
 
m ali
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Chandra,

I got it.
 
Zhao zhenhua
Ranch Hand
Posts: 37
Firefox Browser IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ,Chandra Bhatt.Yes!You are right!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic