I have the following situation: I have a LinkedHashMap<> where the key type is a String and the values types varies: double, String, LinkedHashMap, etc. I am trying to extract a value from a key of one of the LinkedHashMaps values which are a value of the main map. For example, I'd like to get the result 1 from the following code (obviously it is a mess since it doesn't even compile):
Map<String, Object> input = new HashMap<>();
input.put("a", "1234");
input.put("b", "2345");
input.put("c", "3456");
input.put("d", new HashMap<String, String>());
HashMap<String, Object> input2 = (HashMap<String, Object>)(input.get("d"));
input2.put("d1", 1);
input2.put("d2", 2);
Optional<Integer> result = input.entrySet().stream()
.filter(e -> e.getKey().equals("d"))
.map(Map.Entry::getValue)
.filter(e -> e.getKey().equals("d1"))
.findFirst();
Where do I go wrong, and of course, what is the best way to get the result?
Thanks.
Integer result = ((Map)input.getOrDefault("d", Collections.emptyMap())).get("d1");
– Holger Apr 2 '19 at 12:18