Code-Apprentice
Code-Apprentice

Reputation: 83587

Filtering a List in Java

In Haskell, I can do

filter pred list

To create a new list with elements of list for which the function pred is true. Does the Java API have something similar for java.util.List or other collections? I haven't been able to find anything in the API docs.

Upvotes: 0

Views: 261

Answers (2)

Anatoli
Anatoli

Reputation: 932

As you maybe able to see from Java doc for List. There is no filter for core API.

I would just iterate through the list and pull whatever you need into a new list.

Upvotes: 1

Paul Bellora
Paul Bellora

Reputation: 55233

The core API does not, but Guava has Collections2.filter, which takes a Collection and Predicate and returns a view of the filtered elements (which you are free to copy into a new List).

Upvotes: 5

Related Questions