aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question about TreeSet and HashSet Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Question about TreeSet and HashSet" Watch "Question about TreeSet and HashSet" New topic
Author

Question about TreeSet and HashSet

Arsineh Keshishian
Greenhorn

Joined: Apr 21, 2008
Posts: 19
Hi guys,

I found out that after adding Objects, like Person, into a TreeSet a ClasCastException is trown at runtime when no Comparator is defined for the TreeSet...

The second thing is that TreeSet when using Comparator doesn't allow dupplicats.
So running following Code

public class Group extends TreeSet<Person> {

public static void main(String[] args) {

MyComperator myComp = new MyComparator();

Group g = new Group(myComp);
g.add(new Person("Hans"));
g.add(new Person("Lotte"));
g.add(new Person("Jane"));
g.add(new Person("Hans"));
g.add(new Person("Jane"));
System.out.println("Total: " + g.size());
{
public boolean add(Person o) {
System.out.println("Adding: " + o.getName());
return super.add(o);
}

Group(Comparator c)
{
super(c);
}
}
prints out:
Adding: Hans
Adding: Lotte
Adding: Jane
Adding: Hans
Adding: Jane
Total: 3

Ok, it means second "Hans" and "Jane" were not added into the TreeSet.
But when I add the same stuff into a HashSet(which hasn't overriden hashcode and equals) I get a total of 5 items.

My question is now
1. HashTree needs a comparator to not throw an exception in runtime
and
2. HashTree with a comparator doesn't add dupplicate items even it hasn't overriden hascode and equals. Is that right?

Thank you
Arsineh
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Arsineh,

Welcome to the JavaRanch.

When you post code, please post it using the UBB Code tag, which will make it far easier to read. It'll also help if you post code that actually compiles.

By definition, a class that implements the Set<E> interface is a collection that contains no duplicates. Both HashSet<E> and TreeSet<E> implement Set<E>.

If you refer to the API, you'll see this formally means that a set contains no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element.

If no equals is implemented for the elements you are comparing, equals defaults to the Object implementation, which merely compares the object references. So in your HashSet question, because you have no Equals for your objects, as long as the object references are different, they will be added to the set.

The API for TreeSet explains that if you use the default constructor TreeSet<E>(), that the elements inserted must implement the Comparable interface and must be mutually comparable, otherwise add will throw a ClassCastException.

When you use the TreeSet<E>(Comparator<? super E> c) constructor, the elements added must be mutually comparable using the Comparator, or they will throw a ClassCastException.


There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about TreeSet and HashSet
 
Similar Threads
what is the correct answer?
K&B Self Test Pg 617, Q12- HashSet doubt
Correctly override equals and hashcode
HashSet
Generics K&B doubt