SoFunction
Updated on 2025-04-13

Summary of several common ways of merging collections in Java (List, Set, Map)

Java provides a variety of ways to merge sets (List, Set, Map), and the most suitable method can be selected in different scenarios. The following is a compilation of common merging collection methods:

1. Merge List

1.1 addAll()

  • Applicable scenarios: Directly merge two Lists
  • Features: Modify the original collection without creating a new collection
  • Example
List<String> list1 = new ArrayList<>(("A", "B", "C"));
List<String> list2 = ("D", "E");

(list2);
(list1); // [A, B, C, D, E]

1.2 ()

  • Applicable scenarios: No modification to the original collection, return to the new List
  • Features: Streaming operation, suitable for immutable collections
  • Example
List<String> mergedList = ((), ())
                                .collect(());
(mergedList); // [A, B, C, D, E]

1.3 () + flatMap()

  • Applicable scenarios: Merge multiple Lists
  • Features: Multiple Lists can be merged at the same time
  • Example
List<String> list3 = ("F", "G");

List<String> mergedList = (list1, list2, list3)
                                .flatMap(Collection::stream)
                                .collect(());

(mergedList); // [A, B, C, D, E, F, G]

1.4 ()

  • Applicable scenarios: Create an immutable List
  • Features:returnImmutablegather
  • Example
List<String> mergedList = (((), ())
                                           .collect(()));
(mergedList); // [A, B, C, D, E]

2. Merge Set

2.1 addAll()

  • Applicable scenarios: Merge Set (no duplicate elements)
  • Example
Set<Integer> set1 = new HashSet<>((1, 2, 3));
Set<Integer> set2 = new HashSet<>((3, 4, 5));

(set2);
(set1); // [1, 2, 3, 4, 5]

2.2 ()

  • Applicable scenarios: No modification to the original collection
  • Example
Set<Integer> mergedSet = ((), ())
                               .collect(());
(mergedSet); // [1, 2, 3, 4, 5]

2.3 () + flatMap()

  • Applicable scenarios: Merge multiple Sets
  • Example
Set<Integer> set3 = new HashSet<>((6, 7));

Set<Integer> mergedSet = (set1, set2, set3)
                               .flatMap(Set::stream)
                               .collect(());

(mergedSet); // [1, 2, 3, 4, 5, 6, 7]

3. Merge Map

3.1 putAll()

  • Applicable scenarios: Directly merge Map
  • Features: If the key exists, the latter will override the former
  • Example
Map<String, Integer> map1 = new HashMap<>();
("A", 1);
("B", 2);

Map<String, Integer> map2 = new HashMap<>();
("B", 3);
("C", 4);

(map2);
(map1); // {A=1, B=3, C=4}

3.2 merge()

  • Applicable scenarios: Process duplicate key when merging
  • Features: You can specify the key processing method
  • Example
((key, value) ->
    (key, value, Integer::sum)
);
(map1); // {A=1, B=5, C=4}

3.3 compute()

  • Applicable scenarios: Dynamic calculation key
  • Example
((key, value) ->
    (key, (k, v) -> (v == null) ? value : v + value)
);
(map1); // {A=1, B=5, C=4}

3.4 Stream API

  • Applicable scenarios: Do not modify the original map, generate a new map
  • Example
Map<String, Integer> mergedMap = (map1, map2)
    .flatMap(m -> ().stream())
    .collect((
        ::getKey, 
        ::getValue, 
        Integer::sum));

(mergedMap); // {A=1, B=5, C=4}

4. Summary

Data structure method Applicable scenarios Whether to modify the original collection
List addAll() Direct merge
() No modification to the original List
() + flatMap() Merge multiple Lists
() Create an immutable List
Set addAll() Direct merge
() Don't modify the original Set
() + flatMap() Merge multiple Sets
Map putAll() The latter covers the former
merge() Merge the same key
compute() Calculate key value
Stream API No modification to the original map
  • ✅ Modify the original collection: Suitable for scenarios where existing collections are directly updated.
  • ❌ Do not modify the original collection: Suitable for functional programming and immutable collection scenarios.

These methods are suitable for different development needs. Which method is chosen depends on business logic and performance requirements.

This is the article about several common ways of Java merge collection (List, Set, Map). For more information about Java merge collection methods, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!