简体   繁体   English

根据条件合并两个列表并使用 java 8 将结果推送到 map

[英]Merge Two Lists based on a condition and push the result to a map using java 8

I have two lists source and target want to merge them based on some condition and push the data to Hashmap.我有两个列表源和目标想要根据某些条件合并它们并将数据推送到 Hashmap。 I tried below code but i could not succeed.我尝试了下面的代码,但我无法成功。

public List<Persona> fetchCommonPersonas(List<User> sourceList,
                                             List<User> targetList) {
final Map<String, String> map = new HashMap<>();
       map = sourceList.stream()
                .filter(source -> targetList.stream().anyMatch(destination -> {
                    if(destination.getAge().equals(source.getAge())) {
                        map.put(source.getUserId(), destination.getUserId());
                    }
                }
                ));    
}

Here's one way of doing it:这是一种方法:

Map<String, String> map = 
    sourceList.stream()
              .map(source -> targetList.stream()
                                       .filter(dest -> dest.getUserId().equals(source.getUserId()))
                                       .map(dest -> new SimpleEntry<>(source.getPersonaId(), dest.getPersonaId()))
                                       .firstFirst())
              .filter(Optional::isPresent)
              .map(Optional::get)
              .collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue));   

You find for each element of the source list a corresponding element of the target list, map these elements to a Map.Entry that contains the two person Ids, and collect all the entries to a Map .您为源列表的每个元素找到目标列表的对应元素 map 这些元素到包含两个人员 ID 的Map.Entry ,并将所有条目收集到Map

You can utilize a groupingBy of the source list to look up for the data in the second stage and then collect the target and source id pairs as follows -您可以利用源列表的groupingBy在第二阶段查找数据,然后collect目标和源 id 对,如下所示 -

Map<Integer, List<String>> sourceGrouping = sourceList.stream()
        .collect(Collectors.groupingBy(User::getAge,
                Collectors.mapping(User::getId, Collectors.toList())));

Map<String, String> map = targetList.stream()
        .filter(u -> sourceGrouping.containsKey(u.getAge()))
        .flatMap(u -> sourceGrouping.get(u.getAge())
                .stream().map(s -> new AbstractMap.SimpleEntry<>(s, u.getId())))
        .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, 
                AbstractMap.SimpleEntry::getValue));

After i got inputs from Eran this the final piece of code在我从 Eran 得到输入后,这是最后一段代码

Map<String, String> commonMap = sourceList.stream()
        .flatMap(source -> targetList.stream()
        .filter(target -> source.getUserId().equals(target.getUserId()))
        .map(target -> new AbstractMap.SimpleImmutableEntry<>(sourcePersona.getPersonaId(), targetPersona.getPersonaId())))
        .filter(immutableEntry -> (immutableEntry != null
                && StringUtils.isNotBlank(immutableEntry.getKey()) && StringUtils.isNotBlank(immutableEntry.getValue())))
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

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

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