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