Got a Java List object and wanna trim it down to a more manageable size?

If so, then you've come to the right place. I'll show you how to get rid of the objects you don't want.

Even better: you won't need to do any looping or streaming. You can cut that List down to size with just one line of code.

With removeIf()

One of the best ways to separate the wheat from the chaff in your List object is to use the removeIf() method.

That's a great option because you can do while keeping your code concise.

No need to worry about for loops or complex streaming with filters. Just a simple one-liner with a Predicate.

Speaking of Predicate, that's what removeIf() takes as its sole parameter.

If you're not familiar with Predicate, it's a functional interface that returns a boolean. In the case of removeIf(), the Predicate identifies elements in the List that will get eliminated.

By the way: I keep using List as an example here, but removeIf() works on any object that implements Collection. So feel free to use it with TreeSet, LinkedList, Stack, etc.

But List seems to be the most common usage.

Trimming the Fat

Okay, let me show you how to make use of this removeIf() thing.

Suppose you've got a model class that looks like this:

public class Employee {

    private String id;    
    private String lastName;
    private String firstName;

//getters and setters

}

And then you've got some test data that's created with this code:

List<Employee> employees = new ArrayList<>();

Employee employee1 = new Employee();
employee1.setId("A17");
employee1.setFirstName("Frank");
employee1.setLastName("Smith");
employees.add(employee1);

Employee employee2 = new Employee();
employee2.setId("B12");
employee2.setFirstName("Bunny");
employee2.setLastName("Cheshire");
employees.add(employee2);

Employee employee3 = new Employee();
employee3.setId("C11");
employee3.setFirstName("Glenn");
employee3.setLastName("Kin");
employees.add(employee3);

That's going to give you a List of three (3) employees.

Now let's further assume that you want to get rid of any employee whose last name begins with K.

Here's how you'd make that happen:

employees.removeIf(employee -> employee.getLastName().startsWith("K"));

The code invokes the removeIf() method on the List object. The Predicate here is this part:

employee -> employee.getLastName().startsWith("K")

The Predicate is coded as a lambda expression. It returns true for any employee whose last name starts with K.

Now pay attention here: when the Predicate returns true, that means the object is removed from the List.

That might not be very intuitive to some people. A few folks might think: "If it's true, it stays in the List."

But it doesn't. If it's true, it gets removed from the List.

That makes sense because the name of the method is removeIf(). So it will remove the object if the Predicate evaluates to true.

Now add this code after the code from above:

employees.forEach(System.out::println);

That's going to print out the following:

playground.Employee@649d209a[firstName=Frank,id=A17,lastName=Smith]
playground.Employee@e580929[firstName=Bunny,id=B12,lastName=Cheshire]

As you can see, the employee with the last name that begins with K has been eliminated from the List.

That's exactly what you'd expect.

Wrapping It Up

It's easy to trim List objects using the removeIf() method. 

Now all you need to do is take what you've learned here and make it your own.

Have fun!

Photo by Tara Winstead from Pexels