aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on TreeSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on TreeSet" Watch "Question on TreeSet" New topic
Author

Question on TreeSet

Rupak Khurana
Ranch Hand

Joined: Mar 01, 2005
Posts: 89
TreeSet ts = new TreeSet();
ts.clear();
ts.add(0.1);
ts.add(3.0);
ts.add(100.0);
ts.add(13.2);
ts.add(5); //<----Throws ClassCastException
System.out.println("The size of TreeSet is = " + ts.size());
itr = ts.iterator();
while(itr.hasNext())
System.out.println(itr.next());

In the code above, the line ts.add(5) throws ClassCastException at runtime. If I change it to a double value 5.0(i.e., Double object after autoboxing), then all is well. My understanding is that argument to the add method of TreeSet should be an object that implements Comparable interface. The TreeSet will try to cast the argument to Comparable and if the cast is illegal then it will throw ClassCastException.

So here the other elements of TreeSet are Double objects, I am trying to add an Integer object, however both are Comparable so the cast should atleast succeed, it may fail later on during comparison. I am not sure how the comparison using compareTo() will work here between Double and Integer objects. Is the Integer object being casted to Double object for comparison ? If yes, then that would surely throw ClassCastException....

Any thoughts....


SCJP 1.5<br />SCWCD 1.4
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

What I think the issue is it is trying to do a primitive conversion and an Auto-boxing at the same time, and the compiler can't do both. It can do one or the other, but not both at the same time.

But I could be wrong.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Rupak]: The TreeSet will try to cast the argument to Comparable and if the cast is illegal then it will throw ClassCastException.

Yes - but this isn't the only casting that gets done. After an object in TreeSet is cast to Comparable, the compareTo() method gets called. For most Comparable classes, the compareTo() method will throw a ClassCastException if the other object being compared to isn't a member of the same class. That isif Apple and Orange both implement Comparable, you still can't compare an Apple to an Orange. You can compare an Apple to an Apple, or an Orange to an Orange. But if you mix the two up, you will get a ClassCastException - that's what is happening here.

Is the Integer object being casted to Double object for comparison ? If yes, then that would surely throw ClassCastException....

Yes, exactly.

As you've seen, this is one of the potential problems with autoboxing - the compiler may not autobox to the type you want it to. One solution is to make sure your numbers are all doubles, by writing 5.0 rather than 5 for example. It's easy tomake a mistake here though, as you've seen. I recommend taking advantage of another 5.0 feature, generics, to provide additional information to the compiler so it will be able to catch errors like this at compile time (rather than waiting for the ClassCastException at run time):


[Mark]: What I think the issue is it is trying to do a primitive conversion and an Auto-boxing at the same time, and the compiler can't do both

Right. In the original code it does the autoboxing to Integer, but no conversion because at compile time, there's no apparent need for a conversion. In my code, the compiler has enough information to tell that autoboxing without conversion is not enough, so it gives a compile-time error to force the programmer to fix the problem.
[ July 12, 2005: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
janki tangeda
Ranch Hand

Joined: Jun 07, 2005
Posts: 54
Is the Auto-boxing feature included in SCJP 1.4 objective?got little confused here..
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by janki tangeda:
Is the Auto-boxing feature included in SCJP 1.4 objective?got little confused here..


Autoboxing was introduced in Java 5.0. So it is not in the SCJP 1.4 objectives.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ritesh Raman
Ranch Hand

Joined: Jun 20, 2005
Posts: 34
hi
Rupak & ALL
coudld u plz. paste the full code of your query.
i try to compile the code ---

import java.util.*;
class Treeset1
{
public static void main(String[] args)
{

TreeSet ts = new TreeSet();

ts.clear();

ts.add(0.1);
ts.add(3.0);
ts.add(100.0);
ts.add(13.2);
ts.add(5);
System.out.println("The size of TreeSet is = " + ts.size());
Iterator itr = ts.iterator();
while(itr.hasNext())
System.out.println(itr.next());


}
}

BUT I FOUND THE COMPILE TIME ERROR.I UNDERSTAND THIS BECAUSE
argument to the add method of TreeSet should be an object.
BUT I DONT KNOW HOW THESE DOUBLE VALUES ARE ADDED IN TREESET?

ANY ONE PLZ. HELP---.......----.....

I ALSO NOT UNDERSTAND THE
Set<Double> ts = new TreeSet<Double>();IS IT WRITE SYNTAX?

IS THIS TYPE OF QUESTION ASK IN SCJP 1.4?

WITH REGARDS
RITESH


Thanks,<br />--------<br />SCJP 1.4
Rupak Khurana
Ranch Hand

Joined: Mar 01, 2005
Posts: 89
Mr Ritesh

That code will compile only with Java 5.0 since it uses autoboxing feature where the compiler will automatically create wrapper object for the primitive value passed to the add().

Dont worry about it if you are focussing on 1.4

Rupak
Rupak Khurana
Ranch Hand

Joined: Mar 01, 2005
Posts: 89
Thank You Jim and Mark for the explanation. Using Generics is a better choice.
Ritesh Raman
Ranch Hand

Joined: Jun 20, 2005
Posts: 34
hi
Rupak

thanks for your response***********************

ritesh.
 
wood burning stoves
 
subject: Question on TreeSet