This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Accessing HashMaps Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Accessing HashMaps" Watch "Accessing HashMaps" New topic

Accessing HashMaps

Jeremy Donaldson

Joined: Jun 21, 2001
Posts: 11

I'm continuing to work on the Assignment OOP-2 and I've come across another delima. I'm trying to access the value of a hashmap by passing a string value. I've since found that it doesn't like that. It obviously requires an argument of type object. Why doesn't automatic promotion work in this case. Then I'm trying to return the value from the hashmap as an int. This again does not work and it doesn't allow me to cast down from an object to an int or Integer.
Here is a portion of the code that compiles but gives me the following runtime exception: java.lang.ClassCastException: java.lang.String
StringTokenizer st = new StringTokenizer(args[0], "-");
int howMany = st.countTokens();

if (howMany == 2) {

String first = st.nextToken();
String second = st.nextToken();
String third = args[1];

Object one = first;
Object two = second;
Object three = third;

Integer temp = (Integer)map.get(one);
int num1 = temp.intValue();
Integer temp2 = (Integer)map.get(two);
int num2 = temp2.intValue();

int num3 = num2+num1;

Integer temp3 = (Integer)map.get(three);
int num4 = temp3.intValue();

Any help is appreciated
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Jeremy,
I am not sure what you are doing. But the following works.

Jeremy Donaldson

Joined: Jun 21, 2001
Posts: 11
I was confused as to what element type you could populate the hashmap with. I'm assuming now that you can populate the hashmap with whatever variable you desire?
Thanks for the help.
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Jeremy,
You can populate the HashMap with any Object you desire (not variable). I don't think it will hold primitive values.
maya pedersen

Joined: May 27, 2004
Posts: 6
I know this thread is some 3+ years old but my question is in the same category.
When you say you can populate Map/HashMap with any object, what exactly do you mean? I mean, I know you can put any object you want to retrieve in it but what about the key? It is also an object but doesn't this object have to be String? I've been trying to implement it with Longs (the object, not the simple type) but I can't get this to work

Please clarify, I need enlightenment


live long and prosper
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
It should work with any object that implements the hashcode() method properly. It should definitely work with Long.

Can you show us some code?

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link:
subject: Accessing HashMaps
Similar Threads
Erroneous sym type What does this mean?
Problem with Sort Method
Swap function in java
Simple Logic Question
function call