6

I have a hashmap

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

which is contained within another hashmap:

Map<Integer, Object> targetFields = new LinkedHashMap<Integer, Object>();

I can see it within debug mode:

20005=0, 453={452-2=7, 452-0=1, 452-1=17, 448-2=81, 448-1=0A, 447-2=D, 447-1=D, 447-0=D, 448-0=0A}, 11=1116744Pq2Q,

where 453 is the Hashmap, however when trying to extract the hashmap from the parent hashmap using:

HashMap <String, Object> grpMap453 = (HashMap)targetFields.get(453);

I'm thrown:

java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.String

Surely the call targetFields.get(453); should simply return a hashmap?

2
  • 4
    The line you show does not match the error message. Commented Jul 7, 2011 at 11:37
  • 1
    Sorry, I'm stumped - I don't see how you'd get that error on that line. Does the stack trace show the problem originating at that line, or somewhere within the get()? You might temporarily try getting 453 as an Object and see what it is you got! Commented Jul 7, 2011 at 11:45

4 Answers 4

11

I've tried making a demo on the basis of what you have described and found no error in it.

    HashMap<String, Object> hashMap = new HashMap<String, Object>();
    hashMap.put("123", "xyz");
    HashMap<Integer, Object> map = new HashMap<Integer, Object>();
    map.put(453, hashMap);
    HashMap<String, Object> newMap = (HashMap<String, Object>) map.get(453);

    System.out.println("Main map "+ hashMap);
    System.out.println("Map inside map "+map);
    System.out.println("Extracted map "+newMap);

It gives warning at line HashMap<String, Object> newMap = (HashMap<String, Object>) map.get(453); that is "Type safety: Unchecked cast from Object to HashMap" but no error at all.

Are you doing the same?

Sign up to request clarification or add additional context in comments.

Comments

0

As already noted you cannot get that error from the line of code where you extract the HashMap. However you will receive that error with following line of code:

String s11 = (String)targetFields.get(453);

Comments

0

To put value in HashMap

HashMap<String, Map<?,?>> hashMap = new HashMap<String, Map<?,?>>();

Map<String, String> internalHashMap  = new HashMap<String, String>()

internalHashMap.put("TEST_KEY1","TEST_Value1");
internalHashMap.put("TEST_KEY2","TEST_Value2");
internalHashMap.put("TEST_KEY3","TEST_Value3");

hashMap.put("TEST_KEY",internalHashMap);

Comments

-1

Instead of :

HashMap <String, Object> grpMap453 = (HashMap)targetFields.get(453);

Try

HashMap <String, Object> grpMap453 = (HashMap<String,Object>)targetFields.get(453);

6 Comments

Sorry, I can't see the difference between the two lines of code.
@javanna: may be now you can see the difference.
You need your eyes checked, there is a very distinct difference. One you are simply casting your result to a Hashmap, the other explicitely casts it to a hashmap <String, object>.
Voting down an answer because you don't understand or cannot read is not going to get you much help. Good Luck, to you and your employer.. I feel they will need it.
@user756212 I didn't vote down. Anyway, take a look at the edit history, your two lines were the same. Harry Joy corrected them and now the difference is pretty clear...so, my eyes don't have any problem at all.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.