Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing HashMaps

 
Jeremy Donaldson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeremy,
I am not sure what you are doing. But the following works.

Regards,
Manfred.
 
Jeremy Donaldson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should work with any object that implements the hashcode() method properly. It should definitely work with Long.

Can you show us some code?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic