Remove elements from a Set in Java
This post will discuss how to remove elements from a set in Java based on some specified condition.
1. Using an iterator
We can use the remove() method provided by the Iterator interface that removes the latest element returned by the iterator. Please note we should not modify the set after the iterator is created (except through the iterator’s own remove method); otherwise, a ConcurrentModificationException is thrown.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
// Method to remove elements from a set in Java public static void remove(Set<Integer> ints) { Iterator<Integer> it = ints.iterator(); while (it.hasNext()) { if (it.next() % 2 == 0) { // remove even elements it.remove(); } } } |
2. Using removeAll() method
Here, the idea is to maintain a collection of elements from the original set that matches the given condition. Then we remove those elements from the set using the Set#removeAll() method, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// Method to remove elements from a set in Java public static void remove(Set<Integer> ints) { Set<Integer> collection = new HashSet<>(); for (Integer i: ints) { if (i % 2 == 0) { collection.add(i); } } ints.removeAll(collection); } |
In Java 8, we can do like
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// Method to remove elements from a set in Java 8 public static void remove(Set<Integer> ints) { Set<Integer> collection = new HashSet<>(); ints.stream() .forEach(i -> { if ((i % 2 == 0)) { collection.add(i); } }); ints.removeAll(collection); } |
3. Using Java 8
⮚ Using Collectors
Here we convert the specified set to a sequential Stream, filter the stream and accumulate the elements that match the given condition into a new set using a Collector.
|
1 2 3 4 5 6 7 8 |
// Method to remove elements from a set in Java 8 // This will return a new set instance, and the original set remains unchanged. public static Set<Integer> remove(Set<Integer> ints) { return ints.stream() .filter(x -> (x % 2 != 0)) .collect(Collectors.toSet()); } |
Please note that Collectors#toSet() doesn’t guarantee on the type of the set returned. We can use Collectors#toCollection() instead to specify the desired set type:
|
1 2 3 4 5 6 7 |
// Method to remove elements from a set in Java 8 public static Set<Integer> remove(Set<Integer> ints) { return ints.stream() .filter(x -> (x % 2 != 0)) .collect(Collectors.toCollection(HashSet::new)); } |
⮚ Using forEach() with Set.remove()
Since we can’t modify a set while iterating over it, we can create a duplicate set and remove elements that satisfy the condition from the original set by iterating over the duplicate set.
The following code uses Java 8 Stream for filtering, but we can also use an iterator or a for-each loop.
|
1 2 3 4 5 6 7 8 |
// Method to remove elements from a set in Java 8 public static void remove(Set<Integer> ints) { Set<Integer> duplicate = new HashSet<>(ints); duplicate.stream() .filter(x -> (x % 2 == 0)) .forEach(ints::remove); } |
The following code performs filtering inside the forEach() method itself:
|
1 2 3 4 5 6 7 8 9 10 11 |
// Method to remove elements from a set in Java 8 public static void remove(Set<Integer> ints) { Set<Integer> duplicate = new HashSet<>(ints); duplicate.stream() .forEach(x -> { if (x % 2 == 0) { ints.remove(x); } }); } |
⮚ Using removeIf()
Java 8 introduced the Set#removeIf() method that uses Iterator#remove() behind the scenes and removes all elements from the set that satisfies the given condition.
|
1 2 3 4 |
// Method to remove elements from a set in Java 8 public static void remove(Set<Integer> ints) { ints.removeIf(x -> (x % 2 == 0)); } |
That’s all about removing elements from a Set in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)