wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes core java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "core java" Watch "core java" New topic
Author

core java

thirupathi katakam
Greenhorn

Joined: May 12, 2008
Posts: 1
import java.util.*;
public class A{
public static void main(String a[]){
Set s = new TreeSet();
s.add(new Person(20));
s.add(new Person(10));
System.out.println("nnnnnn");

}
}
class Person{
Person(int i){}
}

hi All
y i am getting classcastException ..?
wt is wrong..
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Thirupathi,

Welcome to the JavaRanch. Please Use Code Tags and Use Real Words so that we can help you more effectively.

At what line do you get the ClassCastException? What is the method being called? If you check the API for that method call, what does it tell you about when it 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
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

The add method API sure helps, but the description at the top of the TreeSet class also gives you a hint. You are using a TreeSet, whose elements are sorted, so they must be comparable. You have to either use a TreeSet constructor using a comparator, or make you Person class Comparable.


[My Blog]
All roads lead to JavaRanch
Jhon Clay
Greenhorn

Joined: Jul 16, 2007
Posts: 16
This is one of the solutions as given by you. I'm not able to get the second solution you have given Us
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
Originally posted by chandan venkatesh:
This is one of the solutions as given by you. I'm not able to get the second solution you have given Us

What a heck is this compareTo implementation? All objects are equal?is that it?


Manuel Leiria<br /> <br />--------------<br />Peace cannot be kept by force; it can only be achieved by understanding. <br /> Albert Einstein
Jhon Clay
Greenhorn

Joined: Jul 16, 2007
Posts: 16
Manuel Leiria : what the hell?
i just wanted to compile the code given above, i just googled, got a solution , write whatever logic you want to write in compareTo method, i just gave a example.

If you understood the second one, you can say or if you can suggest something new do it.. don't ridicule what you can't do one of these.This is what i know , right or wrong , i said my things , if its wrong correct it don't comment about it
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
Originally posted by chandan venkatesh:
Manuel Leiria : what the hell?
i just wanted to compile the code given above, i just googled, got a solution , write whatever logic you want to write in compareTo method, i just gave a example.

If you understood the second one, you can say or if you can suggest something new do it.. don't ridicule what you can't do one of these.This is what i know , right or wrong , i said my things , if its wrong correct it don't comment about it

Hey Kido, slow down your horses! No mean to offend.
Next time, try to research a little more instead of posting nonsense code (there are thousands of examples/tutorials of compareTo() method). The code you've posted doesn't helo the OP in any way.
Jhon Clay
Greenhorn

Joined: Jul 16, 2007
Posts: 16
Manuel Leiria: why don't you do that instead of defending yourself .
If you can't do that.. you know what to do..
if you don't know what to do ask me i'll tell you.
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1

Chandan,

If you check the javadoc for java.util.TreeSet, it has a constructor which accepts a comparator as argument. This comparator is used for sorting the elements stored in the set. Please try to find solutions for the issues you face with Java rather than engaging in duels with those who try to help you.... What Manuel said was very much valid.

Cheers,
Raj.
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
Originally posted by chandan venkatesh:
Manuel Leiria: why don't you do that instead of defending yourself .
If you can't do that.. you know what to do..
if you don't know what to do ask me i'll tell you.


Y've posted a code that:
1 - Is wrong
2 - You don't understand it

My guess is you've made a simple google search and grab the first thing that appeared and then posted here.

Anyway, no problemo, I'll teach you how to implement the compareTo method:


As you can see, now the objects can be compared in a consistent way.
This was one approach.Now I'll teach you the other:

Suppose I want to have the possibility to sort accordingly the firstName. What I do is create a class comparator:


Now, check the output of:



which give the results expected:

The first was sorted by id and the second sorted by firstName.

Got it?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
Take it easy, please.

Even if somebody has misunderstood the Comparable interface, there are ways and ways of pointing this out. I take your point about not "mean[ing] to offend" but remember this is a website. You are not face to face and you need to be careful about what you post.

Even if think you have been told off rudely about a mistake, please don't overreact.

Remember the policy here on the Ranch: Be Nice.
Jhon Clay
Greenhorn

Joined: Jul 16, 2007
Posts: 16
Thanks Manuel Leiria
I am happy to learn and i am happy you taught me
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
I'm very pleased to see you aren't annoyed, Chandan Venkatesh.
[ May 14, 2008: Message edited by: Campbell Ritchie ]
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
Originally posted by Campbell Ritchie:
Take it easy, please.

Even if somebody has misunderstood the Comparable interface, there are ways and ways of pointing this out. I take your point about not "mean[ing] to offend" but remember this is a website. You are not face to face and you need to be careful about what you post.

Even if think you have been told off rudely about a mistake, please don't overreact.

Remember the policy here on the Ranch: Be Nice.


I'm sorry if I was rude. Once again, no meaning to offend.

Cheers
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
Thank you very much. Apologies accepted.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: core java