Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Simple HashMap

 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure if this goes here or in the Cattle Drive.

I'm trying to develop a HashMap for a Cattle Drive assignment.



Why am I getting Objects instead of Integers? Is there a way to cast the Objects into Integers? Am I even asking the right questions?

Thanks
 
Ranch Hand
Posts: 440
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Paul Ralph ,

What are you trying to achieve ? According to javadoc , the get( String key ) method will always return an Object . But If you want your hashmap to always return an Integer then use parameterized generics like this


However if you look at the back-end , in this example the JVM will do the casting by itself for you , so ultimately casting has to be done since the default return type of get( String key ) is Object.
 
Marshal
Posts: 74757
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is very old‑fashioned code. You should use boxing
myMap.put("Campbell", 123);
 
Campbell Ritchie
Marshal
Posts: 74757
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or the valueOf method
myMap.put("Campbell", Integer.valueOf(123));
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Ralph wrote:Why am I getting Objects instead of Integers? Is there a way to cast the Objects into Integers? Am I even asking the right questions?


Because you are using the raw type HashMap, without using generics. When you use the raw type, the keys and values will be of type Object, and you need to cast them.

The cast would not be necessary if you would use generics:

 
Paul Ralph
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I got the basic HashMap to work. Now I'm trying to get a method to see the HashMap. NumberTable is null inside convert() and I have no idea why.

(I left out some of the code that's outside of the class)




Thanks
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is essentially what's wrong: you're declaring a local variable with the same name as your class variable (NumberTable). The static block has its own scope just like methods do, so when you assign anything to it, your class variable won't be affected at all.

Good luck!
 
Paul Ralph
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, got it. Thanks!
reply
    Bookmark Topic Watch Topic
  • New Topic