Filter And Map
filter keeps elements that satisfy a predicate. map transforms every remaining element into a new value.
Ordering the operations expresses intent and can avoid needless transformations. Here, unrelated filenames are removed before any case conversion occurs.
Keep, then transform
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> files = List.of(
"Main.java", "notes.txt", "App.java");
List<String> result = files.stream()
.filter(file -> file.endsWith(".java"))
.map(String::toUpperCase)
.toList();
System.out.println(result);
}
}
Output:
[MAIN.JAVA, APP.JAVA]
The two Java filenames survive the filter and become uppercase strings in a new list.
Stream.toList() returns an unmodifiable list. Collect into a mutable collection only when later mutation is truly needed.
Keep temperatures above zero, add one to each, and print the new list.