简体   繁体   中英

Merging two list of hashmap using Java8 Stream API

I have two List s of HashMap :

List<HashMap<String,String>> a = new ArrayList<HashMap<String,String>>();
List<HashMap<String,String>> b = new ArrayList<HashMap<String,String>>();

Sample data:

a = [{a1=1, b1=2, c=3},{a2=4, b2=5, c=6}]
b = [{d1=7,c=3},{d2=8,c=6}]

I want to merge the two List s and have a final List of HashMap using Stream API having output:

c = [{a1=1, b1=2, c=3, d1=7},{a2=4, b2=5, c=6, d2=8}]

Any help?

Sometimes the Stream API is not the answer. In this case a regular loop would be much more readable and maintainable. You could even add comments in the loop to explain why it does something without making the code unreadable. Stream API makes mundane things very easy and complicated things even more complicated.

Unless it's a homework assignment in which case it's a stupid homework assignment. School work shouldn't encourage students to use stupid constructs in wrong places.

In the real world readability and maintainability are paramount to line count or cleverness score.

If you merge them according to the List s' index, and both List s have the same length, you can write:

IntStream.range(0,a.size()).forEach(i->a.get(i).putAll(b.get(i)));

This will result in List a containing the merged result.

If you want to produce a new List without mutating the original List s, you can create new HashMap s and collect them to a new List :

List<HashMap<String,String>> c =
    IntStream.range(0,a.size())
             .mapToObj(i -> {
                        HashMap<String,String> hm = new HashMap<>(a.get(i)); 
                        hm.putAll(b.get(i)); 
                        return hm;
                      })
             .collect(Collectors.toList());

EDIT for the updated question:

List<HashMap<String,String>> c =
    a.stream ()
     .map(am -> {
             HashMap<String,String> hm = new HashMap<>(am);
             HashMap<String,String> second =
               b.stream()
                .filter (bm -> bm.get ("c") != null && bm.get ("c").equals (am.get ("c")))
                .findFirst()
                .orElse(null);
             if (second != null) {
               hm.putAll (second);
             }
             return hm;
          })
    .collect(Collectors.toList());

Now we stream over the elements of the first List and for each HashMap , search for the corresponding HashMap of the second List .

Try like this. To simplify it I defined a Bifunction.

BiFunction<HashMap<String, String>, List<HashMap<String, String>>, HashMap<String, String>> biFunction =
            (m1, listOfMap) -> {
                HashMap<String, String> result = new HashMap<>(m1);
                listOfMap.forEach(item -> {
                    if (item.containsKey("c")&& item.get("c").equals(result.get("c")))
                    result.putAll(item);
                });
                return result;
            };

then to merge use this BiFunction to merge List items.

IntStream.range(0, list.size())
            .mapToObj(i -> biFunction.apply(list.get(i), list2))
            .collect(Collectors.toList());

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