Please try this. Assume the first map is foo
and the second map (with LinkedHashMap) is bar
.
bar = new HashMap<>();
for (var key1 : foo.keySet()) {
bar.put(key1, new HashMap<>()); // key1 is of type K1
var innerMap = foo.get(key1); // type: Map< K2, List<Object>>
for (var key2 : innerMap.keySet()) { // key2 is of type K2
bar.get(key1).put(key2, new LinkedHashMap<>());
var innerLinkedMap = bar.get(key1).get(key2); // type: LinkedHashMap<Object, Value>; now empty
for (var element : innerMap.get(key2)) {
innerLinkedMap.put(element, element.something); // insert in the order of the list
}
}
}
Note that it only works if the elements in each list are unique, otherwise the same key would be re-inserted into the LinkedHashMap, which does not affect the insertion order.
I may make some silly mistake in the implementation above, please do comment and edit in that case.