Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

Accessing HashMaps

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
If you like strawberry rhubarb pie, try blueberry rhubarb (bluebarb) pie. And try this tiny ad:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic