aspose file tools*
The moose likes Cattle Drive and the fly likes HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "HashMap" Watch "HashMap" New topic
Author

HashMap

Gary Ba
Ranch Hand

Joined: Oct 23, 2009
Posts: 150

Hi Guys,
I am working on OOP-2 and running into hashmap issue. Here is what I have (in words).

I created a HashMap<String, int>, initialized it to the one I wanted but when I am getting (get()) it using the 'key', the compiler gives me an error which says:
incompatible types.
found: java.lang.Object
required: int

when assigning to an int.

Since I put int on my hashmap, should I not get an int back?
Also, I tried to assign it to Object and compiled okay. The only thing is I need a number in order for me make some multiplication.



Anyone guys,
Garry


star chaser..
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1613
    
  10

So your key is the String and the value is the int?

WAIT.... try wrapping it -- use Integer in your generics declaration instead of int.


When you do things right, people won't be sure you've done anything at all.
Gary Ba
Ranch Hand

Joined: Oct 23, 2009
Posts: 150

It did not work. I tried it on my code and with a simple main test, both did not work. I get the same compile error.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1613
    
  10

So the key is a String and the value is an Integer?

So when you want to access something by key, you would:



Is that the way you're using it?
Gary Ba
Ranch Hand

Joined: Oct 23, 2009
Posts: 150

here is the exact line. number is an int


thank you Janeice
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1613
    
  10

So if the numberString[] array is a String array (and [0] is actually populated with a String), and the key of the HashMap is a String.... and 'number' is an Integer..... and the Map was declared as <String, Integer>.....

it seems you're doing everything right.

I just want to remind you that, well I'm 95% sure, HashMap and other Collections like it (TreeMap, ArrayList) don't hold primitives... they hold wrapper/boxed classes. That could be the issue why you're getting an "Object" and it expects an "int". You may try casting the int 'number' as an Integer, but autoboxing should take care of that. **shrug**

JD

P.S. If your HashMap is really named hashMap, it's not good naming convention. I dunno if the Nitpickers nitpick that, but I will.
Gary Ba
Ranch Hand

Joined: Oct 23, 2009
Posts: 150

do'h.

I found what was wrong with it from your comment. I forget to specify new HashMap<String, Integer>().


Garry
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1613
    
  10

YAY!!!

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashMap
 
Similar Threads
TypeCast from String to HashMap
Putting object in HashMap
Confirmation on HashMap
basics of collections
NaturalLanguageMultiplier