---
title: "What is the best way to filter a Java Collection?"  
description: "What is the best way to filter a Java Collection?"  
author: "Anonymous User"  
published: 2015-08-07  
updated: 2015-08-07  
canonical: https://www.mindstick.com/forum/33402/what-is-the-best-way-to-filter-a-java-collection  
category: "java"  
tags: ["java", "collection"]  
reading_time: 1 minute  

---

# What is the best way to filter a Java Collection?

I want to [filter](https://www.mindstick.com/forum/1176/filter-in-a-xml-file-in-c-sharp) a java.util.Collection based on a predicate.

## Replies

### Reply by Anonymous User

lambdaj allows to filter collections without writing loops or inner classes as in the following example:\

```
List<Person> beerDrinkers = select(persons, having(on(Person.class).getAge(), greaterThan(16)));
```

\
Can you imagine something more readable? You can find it here:http://code.google.com/p/lambdaj/ \
\
With Java 8 update (2014) comes streams and lambdas, solving this problem with a simple one-liner:\

```
List<Person> beerDrinkers = persons.stream()    .filter(p -> p.getAge() > 16).collect(Collectors.toList());
```


---

Original Source: https://www.mindstick.com/forum/33402/what-is-the-best-way-to-filter-a-java-collection

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
