Java Stream API: Intermediate &
Terminal Operations
INTERMEDIATE OPERATIONS
filter()
• When to Use: To remove unwanted elements based on a condition.
• Example:
List<String> names = List.of("Anil", "Ravi", "Arjun");
names.stream()
.filter(n -> n.startsWith("A"))
.forEach(System.out::println); // Output: Anil, Arjun
map()
• When to Use: To transform each element, like converting or formatting.
• Example:
List<String> names = List.of("ravi", "anil");
names.stream()
.map(String::toUpperCase)
.forEach(System.out::println); // Output: RAVI, ANIL
sorted()
• When to Use: To sort the stream elements.
• Example:
List<Integer> nums = List.of(3, 1, 5, 2);
nums.stream()
.sorted()
.forEach(System.out::println); // Output: 1 2 3 5
distinct()
• When to Use: To remove duplicates.
• Example:
List<Integer> nums = List.of(1, 2, 2, 3);
nums.stream()
.distinct()
.forEach(System.out::println); // Output: 1 2 3
limit(n)
• When to Use: To limit to the first n elements.
• Example:
List<String> students = List.of("A", "B", "C", "D");
students.stream()
.limit(3)
.forEach(System.out::println); // Output: A B C
skip(n)
• When to Use: To skip the first n elements.
• Example:
List<String> items = List.of("One", "Two", "Three", "Four");
items.stream()
.skip(2)
.forEach(System.out::println); // Output: Three, Four
flatMap()
• When to Use: To flatten nested collections.
• Example:
List<List<String>> nested = List.of(List.of("A", "B"), List.of("C", "D"));
nested.stream()
.flatMap(List::stream)
.forEach(System.out::println); // Output: A B C D
TERMINAL OPERATIONS
collect()
• When to Use: To gather results into a List, Set, or Map.
• Example:
List<String> names = List.of("Anil", "Ravi", "Arun");
List<String> result = names.stream()
.filter(n -> n.startsWith("A"))
.collect(Collectors.toList());
System.out.println(result); // Output: [Anil, Arun]
forEach()
• When to Use: To perform an action for each element.
• Example:
List<String> list = List.of("One", "Two");
list.stream()
.forEach(System.out::println); // Output: One, Two
count()
• When to Use: To count the number of elements.
• Example:
long count = List.of("Ravi", "Anil", "Meena")
.stream()
.filter(n -> n.length() > 4)
.count();
System.out.println(count); // Output: 1
reduce()
• When to Use: To combine elements into one result.
• Example:
List<Integer> numbers = List.of(1, 2, 3);
int total = numbers.stream()
.reduce(0, (a, b) -> a + b);
System.out.println(total); // Output: 6
anyMatch()
• When to Use: To check if any element matches the condition.
• Example:
boolean found = List.of("Ravi", "Anil")
.stream()
.anyMatch(n -> n.startsWith("A"));
System.out.println(found); // true
allMatch()
• When to Use: To check if all elements match the condition.
• Example:
boolean allStartWithA = List.of("Anil", "Arun")
.stream()
.allMatch(n -> n.startsWith("A"));
System.out.println(allStartWithA); // true
noneMatch()
• When to Use: To check if none of the elements match the condition.
• Example:
boolean result = List.of("Ravi", "Anil")
.stream()
.noneMatch(n -> n.startsWith("Z"));
System.out.println(result); // true
findFirst()
• When to Use: To get the first element.
• Example:
Optional<String> first = List.of("Anil", "Ravi")
.stream()
.findFirst();
System.out.println(first.get()); // Output: Anil
findAny()
• When to Use: To get any one element (useful in parallel streams).
• Example:
Optional<String> any = List.of("One", "Two")
.stream()
.findAny();
System.out.println(any.get()); // Output: One (or any one)