简体   繁体   中英

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. 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 .

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 -

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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