← Back to Gists

Java stream filter map

📝 Java
mcollins
mcollins · Level 3 ·

Filters a stream, transforms each element, and collects the results in Java.

Java
List<String> result = list.stream() .filter(s -> s.startsWith("A")) .map(String::toUpperCase) .collect(Collectors.toList());

Comments

1
charlesl charlesl

Skip the stream and use a plain for loop. It is faster and easier to debug when you inevitably need to handle exceptions.