简体   繁体   English

如何使用 Java Streams 比较两个列表并将共同添加到字段

[英]How to use Java Streams to compare two lists and add common to a field

I have two lists List<Product> products and List<AssetMap> mapAssetIds .我有两个列表List<Product> productsList<AssetMap> mapAssetIds Both the Product and AssetMap classes have two fields productId and assetId . ProductAssetMap类都有两个字段productIdassetId

public class Product {
    public String productId;
    public String assetId;
}

public class AssetMap {
    public String productId;
    public String assetId;
}

List<Product> products = new ArrayList<>();
products.add(new Product("pid1", "aid1"));
products.add(new Product("pid2", "aid2"));
List<AssetMap> assetMaps = new ArrayList<>();
assetMaps.add(new AssetMap("pid560", "aid1"));
assetMaps.add(new AssetMap("pid3", "aid3"));

The output should be: output 应该是:

["pid560", "aid2"]

I want to check if the assetId s in products are present in assetId s in mapAssetIds .我想检查products中的assetId是否存在于assetIdmapAssetIds中。

If they are, then take the productId from mapAssetIds else take the assetId from Product and put them in a List<String> .如果是,则从mapAssetIds中获取productId ,否则从Product中获取assetId并将它们放在List<String>中。

How do I do this using Java Stream?如何使用 Java Stream 执行此操作?

List<Product> products = new ArrayList<>();
    products.add(new Product("aaa"));
    products.add(new Product("bbb"));
    List<AssetMap> assetMaps = new ArrayList<>();
    assetMaps.add(new AssetMap("bbb"));
    assetMaps.add(new AssetMap("ccc"));

    List<String> commons = products.stream().map(Product::getProductId).collect(Collectors.toList());
    commons.retainAll(assetMaps.stream().map(AssetMap::getAssetId).collect(Collectors.toList()));
    for (String common : commons) {
        System.out.println(common);
    }

output "bbb" output "bbb"

You can do like this:你可以这样做:

first create a map from assetMaps so that getAssetId as key and getProductId as value .首先从assetMaps创建一个map ,使getAssetId作为keygetProductId作为value

Map<String, String> map = assetMaps.stream()
      .collect(Collectors.toMap(AssetMap::getAssetId, AssetMap::getProductId));

then stream over products items and find value based on previous result and in case absent value use default value.然后 stream 在products项目上并根据先前的结果查找值,如果缺少值,则使用默认值。

List<String> result = products.stream()
            .map(Product::getAssetId)
            .map(assetId -> map.getOrDefault(assetId, assetId))
            .collect(Collectors.toList());
  List<String> result = new ArrayList<>();
        
        List<String> assestIdsInMap = assetMaps.stream().map(assest -> assest.assestId).collect(Collectors.toList());
        products.stream().forEach(i -> {
            if (assestIdsInMap.contains(i.assestId)) {
                result.add(assetMaps.stream().filter(assetMap -> assetMap.assestId.equals(i.assestId)).findAny().get().productId);
            } else {
                result.add(i.assestId);
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM