• Post Reply Bookmark Topic Watch Topic
  • New Topic

TreeSet add method  RSS feed

 
shyam kumarK
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.*;
public class Test1{
public static void main(String a[]){
Set s = new TreeSet();
s.add(new Person(20));
s.add(new Person(10));
System.out.println(s);
}
}
class Person{
Person(int i){}
}

can anyone tell me why this code gives ClassCastException ?
this exception can only come when its not the case of duplicate entry and
the class of the element cannot be compared with the existing element
Now why it cannot be compared with first element ?
 
Matt Russell
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Person doesn't implement the Comparable interface.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/TreeSet.html#TreeSet()
 
shyam kumarK
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Matt
does it mean that we have to implement and override method compareTo if we want to add objects to TreeSet ?
 
Matt Russell
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shyam kumarK:
Hi Matt
does it mean that we have to implement and override method compareTo if we want to add objects to TreeSet ?

For the default constructor, yes, we need to implement Comparable. However, we've also got the alternative of providing a Comparator object in the constructor.
 
shyam kumarK
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the default constructor, yes, we need to implement Comparable. However, we've also got the alternative of providing a Comparator object in the constructor.

can you tell me how to provide a Comparator object in the constructor.

and also is it same for TreeMap that we need to override compareTo for adding objects?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Thanks,
 
Matt Russell
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shyam kumarK:
is it same for TreeMap that we need to override compareTo for adding objects?

You can easily answer that question for yourself by looking at the API doc:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/TreeMap.html#TreeMap(java.util.Comparator)

(Note that with TreeMap or TreeSet you're not overriding anything when you provide a Comparator.)
 
Matt Russell
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chandra Bhatt:

Thanks for the good example, Chandra. Incidentally I was reading a book this week that pointed out that this implementation of compare() has a problem: namely, the subtraction can underflow or overflow (for example, if p1.i is a large negative number, and p2.i is a large positive number) and return the wrong result. You can use something like this instead:
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!