• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on TreeSet

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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 ]
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the Auto-boxing feature included in SCJP 1.4 objective?got little confused here..
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rupak Khurana
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You Jim and Mark for the explanation. Using Generics is a better choice.
 
Ritesh Raman
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
Rupak

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

ritesh.
 
Legend has it that if you rub the right tiny ad, a genie comes out.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic