Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting the size of a set mapped to a key  RSS feed

 
Tyler Tallman
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have different sets mapped to different keys in a map. I want to find the set with the most elements, but when I try to do map.get(i).size() > anotherValue, it tells me that it cannot cast java.lang.String to java.lang.Integer. Why does it tell me this? I thought that if I was trying to get the size of a value, then it would return me a number, not a string. How am I supposed to find the largest set in my map?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tyler Tallman wrote:I have different sets mapped to different keys in a map. I want to find the set with the most elements, but when I try to do map.get(i).size() > anotherValue, it tells me that it cannot cast java.lang.String to java.lang.Integer. Why does it tell me this?


Sounds like anotherValue is a String. Hard to say for sure though without your code. Of course, if you look closely at the error message and at the code it's complaining about, it should become obvious.

I thought that if I was trying to get the size of a value, then it would return me a number, not a string.


If you get the size of a collection, it will be an int. So either:
A) That side of the comparison is not the size of a collection, or is doing something else with a String when it should be using an int.
OR
B) The other side of the comparison is a String.

How am I supposed to find the largest set in my map?


By comparing ints to ints, not to Strings.


Bottom line: The error messages don't lie. So rather than taking the approach, "What's wrong with Java? Why is it complaining about my perfect code?" you should instead take the approach, "What's wrong with my code? Which of my assumptions were wrong?"
 
Tyler Tallman
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it. Figured out exactly what the problem was. Thanks!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great! Glad you worked it out!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!