Refactor loops to Java Streams and functional patterns
✓Works with OpenClaudeYou are a Java developer specializing in functional programming patterns. The user wants to refactor imperative loops into declarative Java Streams and apply functional composition techniques.
What to check first
- Verify Java version is 8 or higher:
java -version(Streams API requires Java 8+) - Check existing loop structures in your codebase for candidates like
for,while, enhancedfor, and nested iterations that filter, map, or collect data
Steps
- Identify imperative loops that iterate over collections and perform filtering, transformation, or aggregation operations
- Replace
forloops withstream()to create a stream from the source collection - Chain
filter(Predicate<T>)to replace conditional blocks that skip elements - Chain
map(Function<T,R>)to replace operations that transform each element - Use
flatMap(Function<T,Stream<R>>)instead of nested loops that flatten results - Replace collection building logic (like
new ArrayList<>()with.add()in loops) withcollect(Collectors.toList()) - For terminal operations, use
forEach(Consumer<T>)for side effects,reduce()for aggregation, orfindFirst()/findAny()for searching - Chain multiple operations without creating intermediate collections—streams are lazy and evaluate only when a terminal operation is called
Code
import java.util.*;
import java.util.stream.Collectors;
public class StreamRefactoring {
// BEFORE: Imperative loop
public static List<String> filterAndTransformOld(List<String> names) {
List<String> result = new ArrayList<>();
for (String name : names) {
if (name.length() > 3) {
result.add(name.toUpperCase());
}
}
return result;
}
// AFTER: Stream with filter and map
public static List<String> filterAndTransformNew(List<String> names) {
return names.stream()
.filter(name -> name.length() > 3)
.map(String::toUpperCase)
.collect(Collectors.toList());
}
// BEFORE: Nested loops flattening
public static List<Integer> flattenAndFilterOld(List<List<Integer>> matrix) {
List<Integer> result = new ArrayList<>();
for (List<Integer> row : matrix) {
for (Integer value : row) {
if (value > 10) {
result.add(value);
}
}
}
return result;
}
// AFTER: flatMap and filter
public static List<Integer> flattenAndFilterNew(List<List<Integer>> matrix) {
return matrix.stream()
.flatMap(Collection::stream)
.filter(value -> value > 10)
.collect(Collectors.toList());
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Java Skills
Other Claude Code skills in the same category — free to download.
Spring Boot Setup
Scaffold Spring Boot application with REST API
Java Testing
Set up JUnit 5 with Mockito and test containers
Maven/Gradle
Configure Maven or Gradle build system for Java projects
Spring Security
Configure Spring Security with JWT and OAuth2
Spring Data JPA
Set up Spring Data JPA with repositories and entities
Java Docker
Create optimized Docker image for Java/Spring Boot apps
Java Virtual Threads (Project Loom)
Use Java 21+ virtual threads for high-concurrency I/O without the platform-thread overhead
Java Records and Pattern Matching
Use Java 21+ records and pattern matching for cleaner data classes
Want a Java skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.