Skip to content
Back to skills

Java Streams

ASecurity

Modernize imperative Java loops into Streams and replace null checks with Optional

  • 3 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added September 3, 2026
ai-agentsgojavarefactoringapiperformance

Works with

  • api

Security analysis

A100/100

Scanned September 3, 2026

npx -y skills add black141312/ada --skill java-streams --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Java Streams?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Java Streams
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/black141312-java-streams/badge)](https://www.skillsdirectory.com/skills/black141312-java-streams)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: java-streams
description: Modernize imperative Java loops into Streams and replace null checks with Optional
category: languages
---

# Java Streams

Use this when refactoring verbose loop-and-accumulate Java into the Streams API, or replacing null-juggling with `Optional`, without changing behavior.

1. Spot the loop shape: filter/map/reduce/collect patterns over a collection are the prime candidates; a loop with side effects or early break may be clearer left as-is.
2. Translate the pipeline: `for` + `if` + `add` becomes `stream().filter(...).map(...).collect(toList())`; sums/counts become `mapToInt(...).sum()` or `collect(counting())`.
3. Replace grouping/partitioning loops with `Collectors.groupingBy` / `toMap` / `partitioningBy` instead of manually populating maps.
4. Convert methods that may return null to `Optional<T>`, and consume them with `map`/`filter`/`orElseGet`/`ifPresent` rather than `.get()` after an `isPresent` check.
5. Keep lambdas small and side-effect-free; extract complex bodies to named methods or method references (`Type::method`) for readability.
6. Run the existing test suite and confirm output equality, ordering, and null behavior are unchanged.

## Rules
- Streams are for transformation, not iteration with side effects — don't mutate external state inside `forEach`; use `collect` to build results.
- Never call `Optional.get()` without a guarantee; prefer `orElse`, `orElseThrow`, or `ifPresentOrElse`. Don't use `Optional` for fields or method parameters.
- Watch performance: a stream over a tiny list can be slower and noisier than a plain loop — don't convert for its own sake.
- Preserve ordering semantics; `Collectors.toMap` throws on duplicate keys and `groupingBy` returns a `HashMap` (unordered) unless you supply a map factory.
- Avoid stateful or order-dependent lambdas in parallel streams — they'll race; default to sequential unless you've proven a parallel win.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.