Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Simple HashMap

 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • 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
  • 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: 69856
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is very old‑fashioned code. You should use boxing
myMap.put("Campbell", 123);
 
Campbell Ritchie
Marshal
Posts: 69856
278
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
OK, got it. Thanks!
 
Weeds: because mother nature refuses to be your personal bitch. But this tiny ad is willing:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic