hi guys

i have quiestion. i m using hashmap and now what i have to use hashmap value and put that in the int variable, can u guys help with that. i just need some hints so i can work on that. thank you

Recommended Answers

All 10 Replies

HashMap

HashMap map = new HashMap();
map.put("ONE",new Integer(1));

Integer integ = (Integer)map.get("ONE");

int value = integ.intValue();

or

HashMap map = new HashMap<String,Integer>();
        map.put("ONE",1);

        int integ = (Integer)map.get("ONE");

I think you may have meant

Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("ONE", 1);

        int value = map.get("ONE");

Missing type on declaration and redundant cast on map.get(). ;)

thank you for replies so soon. its making more sense for me now, but still there is some confusion, what i have is like this

private Map<Object, String> map= new HashMap <Object, String>();

and i need to create new int variable and use that to keep track of the that hashmap. so if u guys just explain me some more. thanks you.

thank you for replies so soon. its making more sense for me now, but still there is some confusion, what i have is like this

private Map<Object, String> map= new HashMap <Object, String>();

and i need to create new int variable and use that to keep track of the that hashmap. so if u guys just explain me some more. thanks you.

in other words can i do something like this.

int intVariable = map;

so all the hashmap value store in intVariable.

No, that does not make any sense at all. A Map is not an int by any conceptualization. Why do you feel you need to store a map in an int?

No, that does not make any sense at all. A Map is not an int by any conceptualization. Why do you feel you need to store a map in an int?

because that's what i been told to do.

I doubt that is the case. Either you are not describing your question clearly enough or you misunderstood the requirement.

Here's what you can do:

HashMap map = new HashMap();
map.put(new Integer(1), new String("ONE"));
map.put(new Integer(2), new String("TWO"));
map.put(new Integer(3), new String("THREE"));

int one = Integer.parseInt(map.get(new Integer(1)).toString());
and so on...

What on earth is that supposed to accomplish?

I think that you meant to write the other way around. The way you have it, the map.get() returns the String "ONE" and you end up doing: int one = Integer.parseInt("ONE") I think you should try:

HashMap map = new HashMap();
map.put(new String("ONE"), new Integer(1));
map.put(new String("TWO"), new Integer(2));
map.put(new String("THREE"), new Integer(3));

//int one = Integer.parseInt(map.get(new Integer(1)).toString());
int one = map.get("ONE");
and so on...
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.