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