954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

hashmap

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

codered152
Light Poster
36 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

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");
javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

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(). ;)

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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 map= new HashMap ();

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.

codered152
Light Poster
36 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

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 map= new HashMap (); 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.

codered152
Light Poster
36 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

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?

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 
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.

codered152
Light Poster
36 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

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

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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...
new_2_java
Junior Poster
127 posts since Apr 2007
Reputation Points: 7
Solved Threads: 6
 

What on earth is that supposed to accomplish?

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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...
javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You