introduction
In Java programming, sometimes we need to merge two sets of the same type into one. Especially when dealing with lists (List), it is very common to merge two Lists with the same element type into a new List. Here are several effective ways to merge two Java Lists and provide detailed code examples.
1. Use the addAll() method
Method Description
Java'sThe interface provides
addAll()
Method, you can directly add all elements in one collection to another.
Sample code
import ; import ; public class ListMergeExample { public static void main(String[] args) { // Create two lists to be merged List<String> list1 = new ArrayList<>(); ("Apple"); ("Banana"); List<String> list2 = new ArrayList<>(); ("Cherry"); ("Date"); // Use addAll() method to merge two List (list2); ("Merged List: " + list1); // Output the merged list } }
result
Merged List: [Apple, Banana, Cherry, Date]
2. Use concat() method (Java 8 and above)
Starting with Java 8, you can take advantage of the Stream APIconcat()
Method to merge two streams and then merge two Lists.
Sample code
import ; import ; import ; public class ListMergeExample { public static void main(String[] args) { // Create two lists to be merged List<String> list1 = ("Apple", "Banana"); List<String> list2 = ("Cherry", "Date"); // Use the() method to merge two Lists List<String> mergedList = ((), ()) .collect(()); ("Merged List: " + mergedList); } }
result
Merged List: [Apple, Banana, Cherry, Date]
3. Use CopyOnWriteArrayList class (thread safe scenario)
In concurrent environments, you can useCopyOnWriteArrayList
class, itsaddAll()
The method also applies to merge lists and ensures thread safety.
import ; public class ListMergeExample { public static void main(String[] args) { // Create two lists to be merged CopyOnWriteArrayList<String> list1 = new CopyOnWriteArrayList<>(); ("Apple"); ("Banana"); CopyOnWriteArrayList<String> list2 = new CopyOnWriteArrayList<>(); ("Cherry"); ("Date"); // Merge two Lists in a thread-safe environment (list2); ("Merged List: " + list1); } }
4. Use the static method of ()
Collections
The class provides static methodsunion()
, can be used to merge two sets, but it should be noted that the method returnsSet
InsteadList
。
import .*; public class ListMergeExample { public static void main(String[] args) { // Create two lists to be merged List<String> list1 = ("Apple", "Banana"); List<String> list2 = ("Cherry", "Date"); // Use() to merge, but note that the return is Set Set<String> mergedSet = ( new HashSet<>(list1)); (list2); // If you need to convert back to List List<String> mergedList = new ArrayList<>(mergedSet); ("MergedList (without duplicates): " + mergedList); } }
Results (no duplicates included)
Merged List (without duplicates): [Apple, Banana, Cherry, Date]
When merging two Java Lists, the most common one isaddAll()
The method is simple, direct and efficient. For scenarios involving large amounts of data or concurrency, you can consider using Stream API or CopyOnWriteArrayList. Also, to ensure that duplicates are removed during the merge process, you can use it in conjunctionHashSet
or()
method.
Summarize
This is the end of this article about four methods of merging two identical List collections in Java. For more related contents of merging two identical List collections in Java, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!