java hashset union code example Example 1: set union java set1.retainAll(set2); // Intersection Example 2: set union java set1.addAll(set2); // Union