aspose file tools*
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Accessing HashMaps" Watch "Accessing HashMaps" New topic
Author

Accessing HashMaps

Jeremy Donaldson
Greenhorn

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
Thanks,
Jeremy
Manfred Leonhardt
Ranch Hand

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

Regards,
Manfred.
Jeremy Donaldson
Greenhorn

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.
Jeremy
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.
Regards,
Manfred.
maya pedersen
Greenhorn

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

-Maya


live long and prosper
Ilja Preuss
author
Sheriff

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: http://aspose.com/file-tools
 
subject: Accessing HashMaps