I found next code in official java docs:
collection.removeAll(Collections.singleton(element));
I couldn't figure out advantages of this approach. Why not usual element removing?
collection.remove(element);
Thanks!
The former removes all occurrences of the element within collection, the latter removes only first occurrence.
Acc. to docs:
consider the following idiom to remove all instances of a specified element, e, from a Collection, c
collection.removeAll(Collections.singleton(element));
while collection.remove(element);
removes a single instance of the specified element from this collection
So, to remove all instances, you've to use a loop construct with latter approach. while with first one it's just one line job.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With