Removing via Collection.removeIf()

Starting with JDK 8, we can reduce the preceding code to a single line of code via the Collection.removeIf() method. This method relies on Predicate, as in the following example:

melons.removeIf(t -> t.getWeight() < 3000);

This time, the ArrayList iterates the list and marks for deletion those elements that satisfy our Predicate. Furthermore, ArrayList iterates again to remove the marked elements and shift the remaining elements.

Using this approach, LinkedList and ArrayList perform in almost an identical fashion.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset