3.2.2 Intermediate Operations Explained
Intermediate operations in Java Streams are operations that transform or filter the elements of a stream and return a new stream. These operations are lazy, meaning they do not process the elements until a terminal operation is invoked. Understanding intermediate operations is crucial for efficiently manipulating data streams in Java SE 11 applications.
Key Concepts
1. Filter
The filter
operation is used to select elements from a stream that match a given predicate. It returns a new stream containing only the elements that satisfy the condition.
Example
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); List<Integer> evenNumbers = numbers.stream() .filter(n -> n % 2 == 0) .collect(Collectors.toList()); System.out.println(evenNumbers); // Output: [2, 4]
2. Map
The map
operation transforms each element of the stream using a given function. It returns a new stream consisting of the results of applying the function to each element.
Example
List<String> names = Arrays.asList("Alice", "Bob", "Charlie"); List<Integer> nameLengths = names.stream() .map(String::length) .collect(Collectors.toList()); System.out.println(nameLengths); // Output: [5, 3, 7]
3. Sorted
The sorted
operation sorts the elements of the stream according to their natural order or a specified comparator. It returns a new stream with the sorted elements.
Example
List<Integer> numbers = Arrays.asList(5, 3, 1, 4, 2); List<Integer> sortedNumbers = numbers.stream() .sorted() .collect(Collectors.toList()); System.out.println(sortedNumbers); // Output: [1, 2, 3, 4, 5]
4. Distinct
The distinct
operation removes duplicate elements from the stream based on the equals
method. It returns a new stream with distinct elements.
Example
List<Integer> numbers = Arrays.asList(1, 2, 2, 3, 3, 3); List<Integer> distinctNumbers = numbers.stream() .distinct() .collect(Collectors.toList()); System.out.println(distinctNumbers); // Output: [1, 2, 3]
5. Limit
The limit
operation truncates the stream to a specified size. It returns a new stream with the first n
elements of the original stream.
Example
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); List<Integer> limitedNumbers = numbers.stream() .limit(3) .collect(Collectors.toList()); System.out.println(limitedNumbers); // Output: [1, 2, 3]
6. Skip
The skip
operation discards the first n
elements of the stream. It returns a new stream with the remaining elements.
Example
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); List<Integer> skippedNumbers = numbers.stream() .skip(2) .collect(Collectors.toList()); System.out.println(skippedNumbers); // Output: [3, 4, 5]
Examples and Analogies
Think of a stream as a conveyor belt in a factory. Intermediate operations are like different stations on the belt that process the items (elements) as they pass by. The filter
operation is like a quality control station that only lets certain items pass. The map
operation is like a station that changes the shape or size of the items. The sorted
operation is like a station that arranges the items in a specific order. The distinct
operation is like a station that removes duplicate items. The limit
operation is like a station that stops the belt after a certain number of items. The skip
operation is like a station that removes the first few items from the belt.
By mastering these intermediate operations, you can efficiently manipulate and transform data streams in your Java SE 11 applications, making your code more readable and maintainable.