Sometimes we know how to use a tool, but we might fail to apply its use at right case. I know these Set methods but never used them for “Math Set” operations. Good to visit basics again 🙂 .
Set<Type> union = new HashSet<Type>(s1); union.addAll(s2); Set<Type> intersection = new HashSet<Type>(s1); intersection.retainAll(s2); Set<Type> difference = new HashSet<Type>(s1); difference.removeAll(s2);
source : https://docs.oracle.com/javase/tutorial/collections/interfaces/set.html