×

Union in Java

Sets.union() method in Java returns an immutable representation of both the union of two sets. Every element that is present in either backup set is included in the set that is returned. When iterating over all the returned sets, each member of set2 that is not also in set1 is iterated over after all of the elements from set1, in that order.

Syntax:

public static <E> 
  Sets.SetView<E> 
    union(Set<? extends E> set1, 
          Set<? extends E> set2)

Return Value: An unchangeable picture of the merging of two sets is what this method returns.

Example 1:

UnionExpl.java

// Guava's Sets.union() function 
// is demonstrated in Java
import com.google.common.collect.Sets;
import java.util.Set;
  
class UnionExpl1 {
  
    // Main code
    public static void main(String[] args)
    {
        // Creating the first set
        Set<Integer>
            s1 = Sets.newHashSet(1,2,3,4,5);
  
        // Creating the second set
        Set<Integer>
            s2 = Sets.newHashSet(3,5,7,9);
  
        // Using Guava's Sets.union() method
        Set<Integer>
            a = Sets.union(s1, s2);
  
        // Displaying the union of set set1 and set2
        System.out.println("Set 1: "
                           + s1);
        System.out.println("Set 2: "
                           + s2);
        System.out.println("Set 1 union Set 2: "
                           + a);
    }
}

Output:

Set 1: [1,2,3,4,5]
Set 2: [3,5,7,9]
Set 1 union Set 2: [1,2,3,4,5,7,9]

UnionExpl1.java

// Guava's Sets.union() function 
// is demonstrated in Java


  
import com.google.common.collect.Sets;
import java.util.Set;
  
class UnionExpl1 {
  
    // Main code
    public static void main(String[] args)
    {
        // Creating the first set
        Set<String>
            s1 = Sets.newHashSet("J", "a", "v", "a", "t");
  
        // Creating the second set
        Set<String>
            s2 = Sets.newHashSet("p", "o", "i", "n", "t");
  
        // Using Guava's Sets.union() method
        Set<String>
            a = Sets.union(s1, s2);
  
        // Displaying the union of set set1 and set2
        System.out.println("Set 1: "
                           + s1);
        System.out.println("Set 2: "
                           + s2);
        System.out.println("Set 1 union Set 2: "
                           + a);
    }
}

Output:

Set 1: [J,a,v,a,t]
Set 2: [p,o,i,n,t]
Set 1 union Set 2: [J,a,v,a,t,p,o,i,n,t]

Related Topics

Java Integer toUnsignedLong() method

The toUnsignedLong() method of Java Integer class returns a long value by simply converting the given argument to long after an unsigned conversion. Syntax public static long toUnsignedLong (int  x) Parameters The parameter ‘x’...

1 minute read.

Java Float Keyword

Float: In general, there are two categories of data types: primitive data types and non-primitive data types. So, float is the data type which is a primitive data type. Declarating the variables and...

3 minutes read.

Bean class in Java

The web applications that are created with the help of the JSP or Java Server Pages generally have fairly more functionality than the web pages that specifically are created with...

5 minutes read.

Switch Case with Enum in Java

From some conditions, the java switch statement executes one statement. Similar to the If-Else-If ladder statement, this can be Byte, short, int, long, enum, string, and some wrapper types like...

4 minutes read.

How to make Java Projects

Ant and Maven are both offered by NetBeans for the development of Java applications. When using Ant, the IDE creates an Ant build script depending on the settings you select...

6 minutes read.

Bedrock vs Java

The popularity of Minecraft, a sandbox video game, has skyrocketed. The scope, level of complexity, and variety of gameplay in this game are enormous, and user-generated content has helped to...

4 minutes read.

Print Pencil Shape Pattern in Java

Another pattern made from asterisk symbols that use loops and other logical concepts is the pencil pattern. It is usually requested to draw a pattern using a program. To write the...

6 minutes read.

Abstract classes in Java

Abstract classes in Java Java uses the 'abstract’ keyword to make a class abstract. Such classes are known as abstract classes, and the process is known as abstraction. In programming language, abstract...

4 minutes read.

Java Command not found

Java Command not found error is displayed if Java is not installed on the computer or if the command prompt cannot find Java.exe to run the program. Our Java software...

3 minutes read.

Program to Implement FLAMES Game in Java

Friends, Lovers, Affectionate, Marriage, Enemies, and Sibling are all represented by the acronym FLAMES, which also serves as the name of a well-known game. Although, it can be entertaining to...

4 minutes read.

How to Convert String to Object in Java

How to Convert String to Object in Java The Object is the super class of all classes. So you can assign a string to Object directly. There are two methods to...

3 minutes read.

Print Matrix Diagonally in Java

The aim is to print the elements of a matrix of size n*n in some kind of a diagonal pattern. Input : mat[3][3] = {{1, 2, 3},                      {4, 5, 6},                      {7,...

3 minutes read.

Java Integer longValue() method

The longValue()  method of Java Integer class returns a long value for this Integer after a widening primitive conversion. Syntax public long longValue() Parameters NA Specified by This method is specified by longValue in class Number Return...

1 minute read.

Concurrent Linked Deque in Java with Examples

Introduction Java's concurrent-linked deque, which holds its items as linked nodes, is unconstrained and thread-safe. Concurrent Linked Deque allows for element removal and addition on both sides because it implements the...

4 minutes read.

HttpURLConnection

HttpURLConnection Protocol It is a standard set of rules that allow electronic devices to communicate with each other. The http protocol The http protocol is for data communication, distributing, collaborating, hypermedia information systems, on the World Wide...

5 minutes read.

Difference between this and super in Java

In this article, you will be very well equipped with the knowledge of this keyword and super keyword in java. You will be able to understand where to implement this...

3 minutes read.

Sealed Class in Java

What is a Sealed Class in Java? In programming, the two main issues that must be taken into account when creating an application are security and control flow. The use of...

5 minutes read.

ConcurrentSkipListSet in Java

ConcurrentSkipListSet is a class that is a part of collection framework in Java.It has been available since Java version 1.6. ConcurrentSkipListSet  is a scalable concurrent NavigableSet implementation based on a ConcurrentSkipListMap.The...

16 minutes read.

Set Up the Environment in Java

The Java is regarded as the pure object-oriented programming language. The Java applications are first compiled into the byte-code and then run by using the JVM. The Java is the...

4 minutes read.

Java BLOB

The two data types used in Java to store binary and big-character objects are BLOB and CLOB. In contrast to other types of data like float, int, double, etc., it...

4 minutes read.