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
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
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?
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
I doubt that is the case. Either you are not describing your question clearly enough or you misunderstood the requirement.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
What on earth is that supposed to accomplish?
Ezzaral
Posting Genius
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
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448