0

So I have a strange issue with a HashMap and not sure how to access the value. I have a HashMap returned to me from an API that is suppose to be HashMap<String, String>. One of the values is actually a LinkedHashMap. I've tried casting it, but since it is a HashMap that is <String, String> it's giving me an error that it's not possible. Is there anyway to get a LinkedHashMap value out of a HashMap that is <String, String>?

I have tried this with no luck: ((HashMap)userInfo.get("key")).get("key");

     Could not complete request <java.lang.ClassCastException: 
java.util.LinkedHashMap cannot be cast to java.lang.String>java.lang.ClassCastException:

java.util.LinkedHashMap cannot be cast to java.lang.String

This is really ugly looking, but I was actually able to get it out of the HashMap with this:

(HashMap) ((HashMap)((HashMap)userInfo).get("picture")).get("data");

Thanks to Jeroen for sending me down the right path.

9
  • 4
    You need to provide more code and the error you get, otherwise your question isn't very clear. Commented Oct 23, 2013 at 15:57
  • LinkedHashMap is a subclass of HashMap, so why not just use the LinkedHashMap as a HashMap? Commented Oct 23, 2013 at 15:58
  • If your library is returning a raw HashMap without generics, it's probably very old and hard to work with. Look for something better. Commented Oct 23, 2013 at 15:59
  • Why do you want to cast it? You can call get on a LinkedHashMap just the same... Commented Oct 23, 2013 at 16:01
  • 1
    You can't put a LinkedHashMap as a value in a HashMap<String,String>. It shouldn't be possible, unless the library was using a raw type without generics. Commented Oct 23, 2013 at 16:25

3 Answers 3

1

Brackets placement.

Try

(((HashMap)userInfo).get("key")).get("key");

You have to cast before you use .get() (assuming your attempt is actually valid and it is just a matter of brackets).

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

3 Comments

Does not work, complains about casting since the HashMap this is working on is <String, String>
Ok I took it one step further and this actually works... even though it is pretty ugly: (HashMap) ((HashMap)((HashMap)userInfo).get("picture")).get("data");
I'll give you the answer since you sent me down the right path :)
1

Convert typed map first to untyped map and then check type of each value. Map interface is implemented by HashMap and LinkedHashMap classes so you'll most likely want to use it instead of more specific types.

HashMap<String, String> typedMap = ...
Map untypedMap = (Map) typedMap;
Object mapValue = untypedMap.get("key");

if(mapValue instanceof Map) {
  // handle as Map
}

if(mapValue instanceof String) {
  // handle as String
}

Comments

0

If it is a LinkedHashMap and you cast it to a HashMap you will have probelms.

You could instead treat it as a Map:

Map m = userInfo.get("key");

2 Comments

Not sure how it would be confusing, in the title and various places it shows it is using generics <String, String> so return value is going to be a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.