Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-15-2008, 01:50 PM
Member
 
Join Date: Jan 2008
Posts: 2
pHew is on a distinguished road
ClassCastException in TreeSet
Hello Java guys,

I have a problem. I keep getting a ClassCastException I don't understand. Let me tell you what happens:

Code:
Set<A> mySet = new HashSet<A>();
(filling mySet with objects of type A)

Now I want to order the Set:

Code:
Set<A> sortedSet = new TreeSet<A>(mySet);
BANG! ClassCastException in TreeMap.compare

I don't understand it. All the objects in mySet are of type A. Why could he be wanting to cast them anyway?

BTW, the toString() method of type A has been rewritten to return the member String name. If that has something to do with it.

Thanks for reading this,
pHew
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-15-2008, 02:11 PM
Member
 
Join Date: Jan 2008
Posts: 2
pHew is on a distinguished road
Yay, I have it! I need to implement the "Comparable" interface. Thanks a lot guys!
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-16-2008, 01:20 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
Yeah, good job! Keep in mind that TreeSets sort out your data, but how else would it know how to sort it unless you have defined compareTo() in the class, right? For example, say i had a BankAccount class, which should be sorted by balance. For all the TreeSet knows, you could want it to be sorted by the account number. That's why you must define compareTo() and make it implement Comparable.

"This class guarantees that the sorted set will be in ascending element order, sorted according to the natural order of the elements ([see Comparable), or by the comparator provided at set creation time, depending on which constructor is used."
__________________
//Haha javac, can't see me now, can ya?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
TreeSet Demonstration Java Tip java.lang 0 04-15-2008 08:34 PM
Working with HashSet and TreeSet Java Tip java.lang 0 04-15-2008 08:34 PM
Using TreeSet JavaForums Java Blogs 0 12-10-2007 09:53 PM
ClassCastException Ed New To Java 2 07-04-2007 06:26 AM
ClassCastException Felissa New To Java 2 07-04-2007 06:06 AM


All times are GMT +3. The time now is 03:48 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org