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:

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

Example 1:

UnionExpl.java

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

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]





Latest Courses