I hope all is good for you.
I have two maps Map<Type1, Type2> and a Map<Type3, Type1>. I want to merge them and have at the end a Map<Type3, Type2>.
How can I do that? I make this code but the result is Map<Type1, Type2>
Map<Type3, Type2> result =
Stream.of(map1.keySet(), map2.values())
.flatMap(Collection::stream)
.toList()
.stream()
.filter(
id ->
map1.containsKey(id)
&& map2.containsValue(id))
.collect(
Collectors.toMap(
map2::get,
map1::get,
(v1, v2) -> StringUtils.isNotEmpty(v1) ? v1 : v2));
Thanks for your help.
map2.forEach((t3, t1) -> result.put(t3, map1.get(t1)));(Map<Type3, Type1>)and using the values(Type1)to look up the corresponding values(Type2)in the first map(Map<Type1, Type2>)