Java – Combine two hashmap lists using the Java8 Stream API

Combine two hashmap lists using the Java8 Stream API… here is a solution to the problem.

Combine two hashmap lists using the Java8 Stream API

I have two HashMaps for List:

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 two Lists and use the Stream API with output to get the final List: of the HashMap

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

Does it help?

Solution

Sometimes the Stream API is not the answer. In this case, the regular loop will be more readable and maintainable. You can even add comments to the loop to explain why it does something without making the code unreadable. The Stream API makes mundane things very simple and complex things even more complex.

Unless it’s homework, it’s stupid homework. School assignments should not encourage students to use silly structures in the wrong places.

In the real world, readability and maintainability are critical to the number of rows or smartness score.

Related Problems and Solutions