Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

core java

 
thirupathi katakam
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jhon Clay
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jhon Clay
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Manuel Leiria
I am happy to learn and i am happy you taught me
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm very pleased to see you aren't annoyed, Chandan Venkatesh.
[ May 14, 2008: Message edited by: Campbell Ritchie ]
 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much. Apologies accepted.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic