Java Program to Calculate union of two sets

Java Program to Calculate union of two sets

What are Sets?

In Java, a set is a collection of unique elements, meaning that no element can be repeated. Sets are implemented in the Java Collection Framework and are a part of the Java standard library.

In this Article, we will write a program to Calculate union of two sets.

Sets in Java :

The Java Set interface, which is a part of the Java Collections Framework, defines the behavior of a set. The interface is implemented by several classes, including HashSet, LinkedHashSet, and TreeSet. These classes provide different implementations of the set interface, each with their own unique characteristics and performance characteristics.

Sets are a type of data structure that stores a collection of unique elements. They are used to store and manipulate a collection of distinct items, such as a list of names or a list of unique words in a document. Sets are commonly implemented in computer programming languages, and the behavior of sets is typically defined by a set interface or abstract class.

Program to Calculate union of two sets using addAll Function :

Run
import java.util.HashSet;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        Set set1 = new HashSet<>();
        set1.add(1);
        set1.add(2);
        set1.add(3);

        Set set2 = new HashSet<>();
        set2.add(3);
        set2.add(4);
        set2.add(5);

        Set union = new HashSet<>(set1);
        union.addAll(set2);

        System.out.println("Union of the two sets: " + union);
    }
}

Output:

Union of the two sets: [1, 2, 3, 4, 5]

This program creates two sets, set1 and set2, and adds some elements to them. Then it uses the addAll() method to add all elements from set2 to set1. This method is used to union the two sets. Finally, it prints the union of the two sets.

Program to Calculate union of two sets using union Function :

Run
import java.util.HashSet;
import java.util.Set;

public class UnionExample {
    public static void main(String[] args) {
        Set set1 = new HashSet<>();
        set1.add(1);
        set1.add(2);
        set1.add(3);

        Set set2 = new HashSet<>();
        set2.add(3);
        set2.add(4);
        set2.add(5);

        Set union = union(set1, set2);

        System.out.println("Union of the two sets: " + union);
    }

    public static  Set union(Set set1, Set set2) {
        Set unionSet = new HashSet<>(set1);
        unionSet.addAll(set2);
        return unionSet;
    }
}
Output:

Union of the two sets: [1, 2, 3, 4, 5]

This program creates two sets, set1 and set2, and adds some elements to them. Then it uses the Sets.union method to calculate the union of the two sets. Finally, it prints the union of the two sets.

In the main() method, we create two sets (set1 and set2) and populate them with some elements. We then call the union() method, passing in set1 and set2, and store the result in the union set. Finally, we print the union set to display the result.

You need to add the Guava library to your project in order to use the Sets class.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription