Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




union( ) FUNCTION


The 'union( )' Function takes the values from both the sets and joins them.


Example :


x = {"Mohan", "Kriti", "Salim"}
y = {"Sia", "Andrew", "Kriti"}
z = x.union(y)
print(z) 


Output :



  {'Mohan', 'Sia', 'Kriti', 'Salim', 'Andrew'}

So, in the above code we have created a 'Set' and initialised to the variable 'x'.


x = {"Mohan", "Kriti", "Salim"}

java_Collections

Then what we have done is, put the names, 'Sia', 'Andrew' and 'Kriti' in another set 'y'.


y = {"Sia", "Andrew", "Kriti"}

java_Collections

And used the 'union( )' Function to join the Set 'x'(With values 'Kriti', 'Mohan' and 'Salim') with the Set 'y'(With values 'Sia', 'Andrew' and 'Kriti').


And assign it to a new variable 'z'.


z = x.union(y)

And since, 'Kriti' is present in both the Sets, 'Kriti' will be added once. Else it might cause a dupicate entry.


java_Collections

And we get the below output,


{'Mohan', 'Sia', 'Kriti', 'Salim', 'Andrew'}