2

Here's my code:

public static void main(String[] args){
    TreeSet<Dog> d = new TreeSet<Dog>();
    d.add(new Dog());
    System.out.println(d.size());
}

class Dog{
}

As you can see, the Dog class is not a Comparable object and I sure did not specify a Comparator to be used for sorting. How does this snippet of code run without exceptions? It printed out 1.

I tried adding another Dog to the TreeSet and it threw a ClassCastException as expected.

EDIT: I'm using Java 6

mpmp
  • 2,409
  • 4
  • 33
  • 46
  • I do get a `ClassCastException` in Java 7. The first element added should be compared to itself. I believe this change was made to catch that edge case. – Sotirios Delimanolis Mar 05 '14 at 16:31
  • Oh, I forgot to state that I was working with Java 6. – mpmp Mar 05 '14 at 16:36
  • add one more element and you will get what you are looking for :) – Mak Mar 05 '14 at 16:41
  • @Mak Yeah I know haha. It's just that I was expecting it to throw the exception once I added a non-Comparable object for the first time :P – mpmp Mar 06 '14 at 16:03
  • @MiguelPortugal :) Java people are working hard to fix such issues as you can see now its fixed in Java 7 and you do get Exception. – Mak Mar 06 '14 at 20:42

3 Answers3

6

There was a change added in Java 7 to fix this. It was an error.

Due to an error in java.util.TreeMap, it was previously possible to insert invalid null elements and elements not implementing Comparable into empty TreeMaps and TreeSets. Only a single invalid element could be inserted into the empty TreeMaps or TreeSets; additional elements would cause the expected NullPointerException or ClassCastException. Most other operations upon the collection would also fail. As of JDK 7, inserting an invalid null element or an element not implementing Comparable into an empty TreeMap or TreeSet throws a NullPointerException.

(TreeSet is implemented with a TreeMap as its underlying data structure.)

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • hmmm, interesting, looks like they didn't update the javadoc for TreeSet though. – Camilo Mar 05 '14 at 16:42
  • @CamiloBermúdez I'm trying to interpret the javadoc you quoted in your answer to make it fit with the change, but it doesn't sound good. They should have updated that too. The implementation just compares the first element to itself. – Sotirios Delimanolis Mar 05 '14 at 16:44
  • I also see this in the JDK 7 source code. Now there is a special case class cast to Comparable, forcing an exception even when there is nothing to compare with. – Audrius Meškauskas Mar 06 '14 at 08:05
2

According to the javadoc that's the expected behavior, it will throw a ClassCastException only if the new element cannot be compared with the current elements in the set:

ClassCastException - if the specified object cannot be compared with the elements currently in this set

EDIT

However, this is valid for JSE6, apparently the javadoc for TreeSet in JSE7 is out of date, since as @SotiriosDelimanolis points out, this problem was solved for JSE7.

Camilo
  • 1,889
  • 2
  • 17
  • 16
0

Very nice trick. You are not getting exception because you didn't added more than one object as only then you need to compare the object which will end up throwing exception.

Mak
  • 596
  • 5
  • 10