Java 8按条件收集两个列表进行映射

时间:2015-08-06 12:10:48

标签: java collections java-8 java-stream

我有一个对象:

public class CurrencyItem {
    private CurrencyName name;
    private BigDecimal buy;
    private BigDecimal sale;
    private Date date;
    //...
}

其中CurrencyName之一是:EUR,USD,RUR等。

两个清单

List<CurrencyItem> currenciesByCommercialBank = ...
List<CurrencyItem> currenciesByCentralBank = ...

如何将此列表合并到Map<CurrencyItem, CurrencyItem>,其中键为currenciesByCommercialBank,值为currenciesByCentralBank且条件为

currenciesByCommercialBank.CurrencyName == currenciesByCentralBank.CurrencyName

2 个答案:

答案 0 :(得分:6)

这应该是最佳的。您首先要建立一个从货币到商业银行的地图。然后你穿过你的中心建立一个从商业到中心的地图(在第一张地图中查找)。

    List<CurrencyItem> currenciesByCommercialBank = new ArrayList<>();
    List<CurrencyItem> currenciesByCentralBank = new ArrayList<>();
    // Build my lookup from CurrencyName to CommercialBank.
    Map<CurrencyName, CurrencyItem> commercials = currenciesByCommercialBank
            .stream()
            .collect(
                    Collectors.toMap(
                            // Map from currency name.
                            ci -> ci.getName(),
                            // To the commercial bank itself.
                            ci -> ci));
    Map<CurrencyItem, CurrencyItem> commercialToCentral = currenciesByCentralBank
            .stream()
            .collect(
                    Collectors.toMap(
                            // Map from the equivalent commercial
                            ci -> commercials.get(ci.getName()),
                            // To this central.
                            ci -> ci
                    ));

答案 1 :(得分:2)

以下代码是O(n 2 ),但对于小型集合(可能是您的列表)应该没问题:

return currenciesByCommercialBank
    .stream()
    .map(c ->
        new AbstractMap.SimpleImmutableEntry<>(
            c, currenciesByCentralBank.stream()
                                      .filter(c2 -> c.currencyName == c2.currencyName)
                                      .findFirst()
                                      .get()))
    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
  }

如果您想声明currenciesByCentralBank包含currenciesByCommercialBank中每个项目的匹配项,则上述内容是合适的。如果这两个列表可能不匹配,那么以下内容是合适的:

currenciesByCommercialBank
    .stream()
    .flatMap(c ->
        currenciesByCentralBank.stream()
                               .filter(c2 -> c.currencyName == c2.currencyName)
                               .map(c2 -> new AbstractMap.SimpleImmutableEntry<>(c, c2)))
    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

在这种情况下,地图将包含所有匹配项,并且不会抱怨缺少条目。