This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple HashMap

 
Paul Ralph
Ranch Hand
Posts: 313
  • 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
 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • 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
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or the valueOf method
myMap.put("Campbell", Integer.valueOf(123));
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15438
41
Android IntelliJ IDE Java Scala Spring
  • 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: 313
  • 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
 
Thomas Kasene
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: 313
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, got it. Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic