English 中文(简体)
两幅不同类型的地图
原标题:Merge two map with different types

我希望大家都对你有利。

我有两幅地图和地图;Type1, 类型2> 地图和图形;Type3, 类型1> 我想把他们合并起来,最后还有地图和地图;Type3, 类型2和特。

我如何能够这样做? 我制定这一法典,但结果为地图和图;图佩1,类型2和特;

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));

感谢您的帮助。

问题回答

我可以考虑的一种方式是围绕第二个地图标的条目(Map<Type3, 类型1 >),并使用<代码>(Type1),在第一个地图<代码>(Map<Type1, 类型2>上查看相应的数值<代码>(Type2)。

Map<Type3, Type2> result = new HashMap<>();
for (Map.Entry<Type3, Type1> entry : map2.entrySet()) {
    Type3 key = entry.getKey();
    Type1 value = entry.getValue();
    Type2 otherValue = map1.get(value);
    result.put(key, otherValue);
}

<代码>containsValue非常昂贵;它是一个完整的通道。 页: 1 不这样做。

收集到地图,要求你设计一个功能,把一个流元素变成一个钥匙,即第二个功能,把一个流元素变为一个价值。 这并非非常困难。 T3仍然是,仅仅需要调整你的T3->T1地图的价值。

import java.util.AbstractMap.SimpleImmutableEntry;

Map<Type3, Type1> map1 = ...;
Map<Type1, Type2> map2 = ...;

Map<Type3, Type2> result = map1.entrySet().stream()
  .map(entry -> new SimpleImmutableEntry<>(entry.getKey(), map2.get(entry.getValue())))
  .filter(entry -> entry.getValue() != null)
  .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

这是:

  • goes through every K/V pair in your T3->T1 map.
  • Turns each such pair into a T3->T2 pair instead, by looking up the T1 value in the T1->T2 map.
  • Assuming you want any k/v pairs in the T3->T1 map where the value is not in the T1->T2 map to just be removed, we then filter it out, (ab)using null as a go-between. We could have filerted with .filter(entry -> map2.containsKey(entry.getValue()) too, but this way we save 1 lookup.
  • These map entries are already exactly what we want, so we just reconstitute that back into a map.




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签