This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Simple HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Simple HashMap" Watch "Simple HashMap" New topic
Author

Simple HashMap

Paul Ralph
Ranch Hand

Joined: Aug 10, 2000
Posts: 312
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

Joined: Aug 11, 2011
Posts: 440

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.


Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
That is very old‑fashioned code. You should use boxing
myMap.put("Campbell", 123);
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
Or the valueOf method
myMap.put("Campbell", Integer.valueOf(123));
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

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

Joined: Aug 10, 2000
Posts: 312
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

Joined: Feb 21, 2013
Posts: 3
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

Joined: Aug 10, 2000
Posts: 312
OK, got it. Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple HashMap