4. Use RemoveIf in Java Collections
Today's tips is to use the
Follow the Java Holiday Calendar 2016 with small tips and tricks all the way through the winter holiday season.
removeIf()
method (that all collection classes like List
have) rather than manually iterating over the elements and remove them. For large data sets, removeIf()
can be orders of magnitudes faster than other ways. It also looks much better in your code. Why? Read more here and see for your self!Do This:
items.removeIf(i -> predicate(i));
Don't Do This:
for (Iterator it = items.iterator(); it.hasNext();) {Read more in the original article at http://javadeau.lawesson.se/2016/09/java-8-removeif.html
if (predicate(it.next())) {
it.remove();
}
}
Follow the Java Holiday Calendar 2016 with small tips and tricks all the way through the winter holiday season.