This example shows how to get a union or intersection of two TreeSet objects in Java. This example also shows how to get a union and intersection of two Sets using the retainAll and addAll methods.

## How to get a union or intersection of two TreeSet (Set) objects in Java?

### How to get a union of two Set objects (TreeSet objects)?

The union of two Set objects is a Set of all elements present in the two Set objects like a TreeSet. The union of Sets is also a Set so it does not have duplicate elements. For example, if set 1 contains [1, 2, 3] and set 2 contains [2, 3, 4] then the union of these two sets will be [1, 2, 3, 4].

We can get a union of two TreeSet objects using the `addAll`

method.

1 | public boolean addAll(Collection<? extends E> collection) |

The `addAll`

method adds all the elements of the specified collection object to this set. If the specified collection has elements that are already present in this set, they are not added.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import java.util.TreeSet; public class TreeSetUnionIntersectionExample { public static void main(String[] args) { //TreeSet 1 TreeSet<Integer> tSetNumbers1 = new TreeSet<Integer>(); tSetNumbers1.add(3); tSetNumbers1.add(1); tSetNumbers1.add(2); //TreeSet 2 TreeSet<Integer> tSetNumbers2 = new TreeSet<Integer>(); tSetNumbers2.add(5); tSetNumbers2.add(3); tSetNumbers2.add(4); /* * To get a union of two sets, use the * addAll method */ tSetNumbers1.addAll( tSetNumbers2 ); System.out.println("TreeSet union: " + tSetNumbers1); } } |

Output

1 | TreeSet union: [1, 2, 3, 4, 5] |

As we can see from the output, the element “3” was not added because it was already present in the TreeSet.

### How to get an intersection of two Set objects (TreeSet objects)?

The intersection of two sets A and B is the set of all elements of set A that are also present in the set B. In other words, its the set containing all common elements of the given two sets.

For example, the intersection of set [1, 2, 3] and set [2, 3, 4] is [2, 3] i.e. common elements of set 1 and set 2.

We can get the intersection of two Set objects (TreeSet objects) using the `retainAll`

method.

1 | public boolean retainAll(Collection<?> collection) |

The `retainAll`

method removes all elements from this set that are not present in the specified collection. In other words, all the common elements of this set and the specified collection object are kept in this set object, other elements are removed.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import java.util.TreeSet; public class TreeSetUnionIntersectionExample { public static void main(String[] args) { //TreeSet 1 TreeSet<Integer> tSetNumbers1 = new TreeSet<Integer>(); tSetNumbers1.add(3); tSetNumbers1.add(1); tSetNumbers1.add(4); tSetNumbers1.add(2); //TreeSet 2 TreeSet<Integer> tSetNumbers2 = new TreeSet<Integer>(); tSetNumbers2.add(5); tSetNumbers2.add(3); tSetNumbers2.add(6); tSetNumbers2.add(4); /* * To get an intersection of two sets, use the * retainAll method */ tSetNumbers1.retainAll( tSetNumbers2 ); System.out.println("TreeSet intersection: " + tSetNumbers1); } } |

Output

1 | TreeSet intersection: [3, 4] |

This example is a part of the TreeSet in Java Tutorial with Examples.

Please let me know your views in the comments section below.

References:

Java 8 TreeSet

## Add Comment