Setset1 = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5)); Set set2 = new HashSet<>(Arrays.asList(3, 4, 5, 6, 7)); set1.retainAll(set2); System.out.println(set1); // Output: [3, 4, 5]
SetThe Java Set retainAll method is part of the java.util package, which is a standard library.numbers = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); int divisor = 2; numbers.removeIf(num -> num % divisor != 0); System.out.println(numbers); // Output: [2, 4, 6, 8, 10]