What is the best way to filter a Java Collection?

Ads
Ads

What is the best way to filter a Java Collection?


Java Coder
What is the best way to filter a Java Collection?
2 Answer(s)      a year and 2 months ago
Posted in : Java Beginners
Ads

Hi,

I have a collection of Person class with following main field:

int id; String personName; int personAge;

and other details.

I want to filter the data and get data of all the persons having personAge >14.

How achieve this? What is the best way to filter a Java Collection?

Thanks

View Answers

April 13, 2017 at 9:31 AM


Hi,

In Java 8 you can use following code:

List<Person> filteredPersons = persons.stream()
    .filter(p -> p.getPersonAge() > 14).collect(Collectors.toList());

Above code is using Java 8.

Thanks



April 13, 2017 at 9:32 AM


Hi,

We have huge collection of Java util package examples check all at Java Util Examples.

Thanks


Ads