Skip to Content
Java Coding Problems
book

Java Coding Problems

by Anghel Leonard
September 2019
Intermediate to advanced
816 pages
18h 47m
English
Packt Publishing
Content preview from Java Coding Problems

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.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Java Coding Problems - Second Edition

Java Coding Problems - Second Edition

Anghel Leonard
Java in a Nutshell, 7th Edition

Java in a Nutshell, 7th Edition

Benjamin J. Evans, David Flanagan
The Well-Grounded Java Developer, Second Edition

The Well-Grounded Java Developer, Second Edition

Benjamin Evans, Martijn Verburg, Jason Clark

Publisher Resources

ISBN: 9781789801415Supplemental Content