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?
-
1What if there's a conflict?shmosel– shmosel2018年08月09日 06:01:26 +00:00Commented Aug 9, 2018 at 6:01
3 Answers 3
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
.
-
I want to have internal hashmap merging on key "c". See my updated question. Can you please help me?Anita Patel– Anita Patel2018年08月09日 06:11:34 +00:00Commented Aug 9, 2018 at 6:11
-
@AnitaPatel does it mean, for example, that the first HashMap from list a could be merged with the second HashMap from list b?Eran– Eran2018年08月09日 06:13:50 +00:00Commented Aug 9, 2018 at 6:13
-
Yes. First HashMap from list a could be merged with the second HashMap from list b by key "c" having same value.Anita Patel– Anita Patel2018年08月09日 06:16:21 +00:00Commented Aug 9, 2018 at 6:16
-
@AnitaPatel and what if multiple HashMaps of the same list have the same value for key "c"?Eran– Eran2018年08月09日 06:17:49 +00:00Commented Aug 9, 2018 at 6:17
-
Data have surety. That case will not raise.Anita Patel– Anita Patel2018年08月09日 06:22:12 +00:00Commented Aug 9, 2018 at 6:22
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());
Explore related questions
See similar questions with these tags.