1. Introduction

In Java, forming the union of two lists means to combine all unique elements from both lists into a single list. This blog post will guide you through how to perform a union operation on two lists using Java’s addAll() method and the stream() API.

2. Program Steps

1. Import the necessary classes.

2. Create two Lists with some common and distinct elements.

3. Use the addAll() method to combine both lists.

4. Remove the duplicate elements to get the union of the lists.

5. Alternatively, use the stream() API along with concat() and distinct() to get the union of the lists.

3. Code Program

// Step 1: Importing necessary classes
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ListUnion {

    public static void main(String[] args) {

        // Step 2: Creating two Lists with some common and distinct elements
        List<Integer> list1 = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
        List<Integer> list2 = new ArrayList<>(Arrays.asList(3, 4, 5, 6, 7));

        // Printing the original lists
        System.out.println("List1: " + list1);
        System.out.println("List2: " + list2);

        // Step 3: Using addAll() method to combine both lists
        List<Integer> union = new ArrayList<>(list1);
        union.addAll(list2);

        // Step 4: Removing duplicate elements to get the union
        List<Integer> finalUnion = union.stream().distinct().collect(Collectors.toList());
        System.out.println("Union using addAll and distinct: " + finalUnion);

        // Step 5: Using stream API to get the union of the lists
        List<Integer> streamUnion = Stream.concat(list1.stream(), list2.stream())
                                          .distinct()
                                          .collect(Collectors.toList());
        System.out.println("Union using stream API: " + streamUnion);
    }
}

Output:

List1: [1, 2, 3, 4, 5]
List2: [3, 4, 5, 6, 7]
Union using addAll and distinct: [1, 2, 3, 4, 5, 6, 7]
Union using stream API: [1, 2, 3, 4, 5, 6, 7]

4. Step By Step Explanation

Step 1: Import the necessary classes and interfaces.

Step 2: Define two List objects, list1 and list2, which contain both common and distinct elements.

Step 3:

– Create a new ArrayList named union and initialize it with the elements of list1.

– Use the addAll() method on union, passing list2 as the parameter. This method adds all of the elements in the specified collection to the end of this list.

Step 4: To remove duplicates and get the final union of the lists, use stream() on the union list, call distinct() to remove duplicates, and then collect the results into a new list finalUnion.

Step 5: Alternatively, you can use the stream() API to perform the union operation directly.

– Use Stream.concat() to concatenate the streams of list1 and list2.

– Call distinct() to eliminate duplicate elements.

– Collect the results into a new list streamUnion.