Skip to content
Advertisement

How to Get data from nested LinkedHashMap

How to get the “test” data from this linkedHashMap

JavaScript

response.get(“url”) = null

response.get(“data.images.original.url”) = null

response.get(“data”).get(“images”).get(“original”).get(“url”)

JavaScript

toString();

result{data={images={original={url=test}}}};

Advertisement

Answer

The “right” way would be

JavaScript

And yes, this is ugly as all heck. Mostly because of the types you’ve chosen.

By making the value of your maps Object you’ve gained flexibility on what to put in, but you put the burden on the “reader” of the map to actually cast to whatever they want. And since generics are involved all of these casts are unchecked (i.e. the runtime can check that the returned objects are in fact Map but it can’t verify that they are really Map<String, Object>.

Additionally, you also make use of double brace initialization, which I’d strongly discourage, because it’s an ugly hack. If you’re using Java 9 or later, then simply use Map.of() instead:

JavaScript

The indentation here is only there to visualize the structure of those calls, it’s absolutely not necessary.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement