What is the best way to filter a Java Collection?

What is the best way to filter a Java Collection?

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









Related Tutorials/Questions & Answers:
What is the best way to filter a Java Collection?
What is the best way to learn java
Advertisements
What is the best way to write to file in Java?
What is the best way to learn Java on my own?
What is the best way to learn Bootstrap?
What is the best way to learn Bootstrap?
What are the best way to learn Spring & Hibernate Framework for a Java beginner?
What are the best way to learn Spring & Hibernate Framework for a Java beginner?
What are the best way to learn Spring & Hibernate Framework for a Java beginner?
What is the best way to learn JavaScript?
What is the best way to learn JavaScript?
What is the best way to learn JavaScript?
What is the best way to learn spring boot
What is the best way to learn spring boot
What is the best way to create a string array in python
What's the best way to learn data science as a beginner?
Best way to reading file in java
Best way to learn Java programming
what is collections?
What is the best path to learn Java?
What is the best path to learn Java?
Best way to learn Java
What are the best online advanced Java tutorials?
What is an efficient way to implement a singleton pattern in Java?
What are some way to learn Java quickly?
What is the fastest way to read a file in Java?
What are the best online Java programming courses for beginners?
What is the best reference to start learning Java?
What is the best reference to start learning Java?
What is the best reference to start learning Java?
What is the best reference to start learning Java?
What is the best reference to start learning Java?
What is the best reference to start learning Java?
What is the best site for advanced Java programming
Best way to learn R Programming
best way to learn data science
best way to learn machine learning
the best way to learn machine learning
the best way to learn data science
best way to study data science
Best way to learn Big Data
What is the best in Ajax Toolkit?
Which is the best way to travel around Delhi
the best way to generate a universally unique object ID.
What degree is best for AI?
What is the fastest way to learn AI?
best way to learn python for data science
best way to learn statistics for data science
best way to learn machine learning quora
best way to learn data science online

Ads