# filter()

say it: **“FIL-ter”** **/ˈfɪltər/**

A stream operation that keeps only elements passing a test.

## What does **filter()** do in Java?

**filter()** — A stream operation that keeps only elements passing a test.

```java
import java.util.List;

class NameFilter {
    List<String> shortNames(List<String> names) {
        return names.stream()
                .filter(name -> name.length() < 5)
                .map(String::toUpperCase)
                .toList();
    }
}
```

Level: Freelancer · [More Java terms at this level](https://www.ask-codey.com/glossary?category=java&level=4)

Updated: November 17, 2026

## In other subjects

- [filter() in JavaScript](https://www.ask-codey.com/glossary/filter-javascript)
- [filter() in Python](https://www.ask-codey.com/glossary/filter)

Source: https://www.ask-codey.com/glossary/filter-java
