• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

ClassCastException after taking input of first key-value pair  RSS feed

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code.



I also wanted to know what this bit of code exactly does? I'm confused that how can I actually get a Set of a Map? I am not sure what actually happens there but I found this to be the way to iterate over Maps.


Thank you. Any help is appreciated.
 
Marshal
Posts: 61690
192
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have got rid of the <b></b> tags for you. I don't know what caused them, but I am told there is a solution in preparation. Sorry about that.
At least your Team objects are immutable, so they can be used as “K”s in a Map, but why are you using a tree map? If you look in its documentation, particularly about this method you will find there is a specific requirement for all types used as “K”s. You will find it impossible to use Team as it stands as a “K” in a tree map.
Consider a hash map instead; that will require a decent hash code method however. Don't use the length of the name as a hash code. Try something like this:-The part before the plus sign may be omitted. Your equals() method is incorrect because it might throw an exception. You must precede the cast with a test for class equality or instanceof. Don't write if (b) return true; else return false; Write return b;Note that will still fail if name is ever null, and you ought to take precautions against nulls. It is important to use || and && not | or &, and the parts must go in that order.
Why do you have a points field in your Team objects? You don't ever seem to use it. What's more, because you have made the class immutable, that field will only ever be correct for the worst team which always loses and therefore has a points count of 0
 
Sheriff
Posts: 21463
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's something else you need to know about TreeMap and TreeSet. Its keys either need to be Comparable, or you need to provide a custom Comparator when constructing the TreeMap / TreeSet. This is how the TreeMap / TreeSet determines order. In your case your class is not Comparable, and you do not provide a Comparator. Because you didn't provide a Comparator the TreeMap still tries to cast your keys to Comparable. That's where the ClassCastException comes from.

I think this is a small flaw in the API of TreeMap and TreeSet combined with some limitations of generics. Ideally the constructors that do not take a Comparator should be bound to only accept T extends Comparable<? super T>. That's not possible with the current generics implementation though. Otherwise you would have gotten a compiler error instead of an exception.
 
Karla Carr
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I have got rid of the <b></b> tags for you. I don't know what caused them, but I am told there is a solution in preparation. Sorry about that.
At least your Team objects are immutable, so they can be used as “K”s in a Map, but why are you using a tree map? If you look in its documentation, particularly about this method you will find there is a specific requirement for all types used as “K”s. You will find it impossible to use Team as it stands as a “K” in a tree map.
Consider a hash map instead; that will require a decent hash code method however. Don't use the length of the name as a hash code. Try something like this:-The part before the plus sign may be omitted. Your equals() method is incorrect because it might throw an exception. You must precede the cast with a test for class equality or instanceof. Don't write if (b) return true; else return false; Write return b;Note that will still fail if name is ever null, and you ought to take precautions against nulls. It is important to use || and && not | or &, and the parts must go in that order.
Why do you have a points field in your Team objects? You don't ever seem to use it. What's more, because you have made the class immutable, that field will only ever be correct for the worst team which always loses and therefore has a points count of 0



Thanks for your answer. I am unsure of how 'name.hashCode()' works. It kinda looks like recursion ending with the StackOverflowException. I also don't get why I necessary need to check ob == this? Isn't that the whole point of overriding the equals() method in Object class? To design our own condition for equality since in Object class, equality is only when two references refer to the same object (==).
 
Karla Carr
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I have got rid of the <b></b> tags for you. I don't know what caused them, but I am told there is a solution in preparation. Sorry about that.
At least your Team objects are immutable, so they can be used as “K”s in a Map, but why are you using a tree map? If you look in its documentation, particularly about this method you will find there is a specific requirement for all types used as “K”s. You will find it impossible to use Team as it stands as a “K” in a tree map.
Consider a hash map instead; that will require a decent hash code method however. Don't use the length of the name as a hash code. Try something like this:-The part before the plus sign may be omitted. Your equals() method is incorrect because it might throw an exception. You must precede the cast with a test for class equality or instanceof. Don't write if (b) return true; else return false; Write return b;Note that will still fail if name is ever null, and you ought to take precautions against nulls. It is important to use || and && not | or &, and the parts must go in that order.
Why do you have a points field in your Team objects? You don't ever seem to use it. What's more, because you have made the class immutable, that field will only ever be correct for the worst team which always loses and therefore has a points count of 0



How is my class immutable? I don't really understand. Could you also be kind enough to answer the last part of my original question? Thank you.
 
Karla Carr
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:There's something else you need to know about TreeMap and TreeSet. Its keys either need to be Comparable, or you need to provide a custom Comparator when constructing the TreeMap / TreeSet. This is how the TreeMap / TreeSet determines order. In your case your class is not Comparable, and you do not provide a Comparator. Because you didn't provide a Comparator the TreeMap still tries to cast your keys to Comparable. That's where the ClassCastException comes from.

I think this is a small flaw in the API of TreeMap and TreeSet combined with some limitations of generics. Ideally the constructors that do not take a Comparator should be bound to only accept T extends Comparable<? super T>. That's not possible with the current generics implementation though. Otherwise you would have gotten a compiler error instead of an exception.

<br /> <br /> Thank you for the answer. Inserting a comparator in the constructor should work then. <br /> Could you also be kind enough to answer the last part of my original question? Thank you.
 
Karla Carr
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:There's something else you need to know about TreeMap and TreeSet. Its keys either need to be Comparable, or you need to provide a custom Comparator when constructing the TreeMap / TreeSet. This is how the TreeMap / TreeSet determines order. In your case your class is not Comparable, and you do not provide a Comparator. Because you didn't provide a Comparator the TreeMap still tries to cast your keys to Comparable. That's where the ClassCastException comes from.

I think this is a small flaw in the API of TreeMap and TreeSet combined with some limitations of generics. Ideally the constructors that do not take a Comparator should be bound to only accept T extends Comparable<? super T>. That's not possible with the current generics implementation though. Otherwise you would have gotten a compiler error instead of an exception.

<br /> <br /> My code doesn't work when I insert a comparator in the constructor. Could you please debug the code for me? Thank you.
I also tried using this. However, I think only a comparator works and not a comparable.

 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!