118. Removing all elements of a collection that match a predicate

Our collection will hold a bunch of Melon:

public class Melon {

private final String type;
private final int weight;

// constructor, getters, equals(),
// hashCode(), toString() omitted for brevity
}

Let's assume the following collection (ArrayList) throughout our examples to demonstrate how we can remove elements from it that match a given predicate:

List<Melon> melons = new ArrayList<>();
melons.add(new Melon("Apollo", 3000));
melons.add(new Melon("Jade Dew", 3500));
melons.add(new Melon("Cantaloupe", 1500));
melons.add(new Melon("Gac", 1600));
melons.add(new Melon("Hami", 1400));

Let's take a look at the different solutions given in the following sections.

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

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