• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

TreeSet add method

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Thanks,
 
Matt Russell
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
You learn how to close your eyes and tell yourself "this just isn't really happening to me." Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic