3

I have two Lists 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 Lists 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?

1
  • 1
    What if there's a conflict? Commented Aug 9, 2018 at 6:01

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

If you merge them according to the Lists' index, and both Lists 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 Lists, you can create new HashMaps 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.

6 Comments

I want to have internal hashmap merging on key "c". See my updated question. Can you please help me?
@AnitaPatel does it mean, for example, that the first HashMap from list a could be merged with the second HashMap from list b?
Yes. First HashMap from list a could be merged with the second HashMap from list b by key "c" having same value.
@AnitaPatel and what if multiple HashMaps of the same list have the same value for key "c"?
Data have surety. That case will not raise.
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.