Common Operations of Stream API
- The Stream API in Java is a powerful tool for working with collections in a functional and declarative way.
- It provides a wide range of operations like filtering, mapping, and reducing, which help process collections with fewer lines of code and improve readability.
- Here’s how to use these operations:
1. Filtering:
- The
filter()
operation is used to filter elements from a stream based on a condition or predicate. Only elements that satisfy the condition are included in the result. - How to Use: Apply
filter()
to a stream and pass a lambda expression that defines the condition. - Example: Filtering Even Numbers
```java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StreamExample {
public static void main(String[] args) {
List numbers \= Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
// Filter even numbers List<Integer> evenNumbers = numbers.stream() .filter(n -> n % 2 == 0) .collect(Collectors.toList());
System.out.println(evenNumbers); // Output: [2, 4, 6, 8]
}
}
```
2. Mapping:
- The
map()
operation is used to transform or map each element of the stream to another form. It applies a function to each element and produces a new stream of transformed elements. - How to Use: Use
map()
to transform elements, passing a function that defines the transformation logic. - Example: Convert each name to uppercase
```java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StreamExample {
public static void main(String[] args) {
List names \= Arrays.asList("John", "Jane", "Jack");
// Convert each name to uppercase List<String> uppercaseNames = names.stream() .map(String::toUpperCase) .collect(Collectors.toList());
System.out.println(uppercaseNames); // Output: [JOHN, JANE, JACK]
}
}
```
3. Reducing:
- The
reduce()
operation combines elements of a stream into a single result. It’s typically used to perform aggregation operations such as summing, finding the maximum, or concatenating strings. - How to Use: Use
reduce()
to aggregate elements by passing an identity value (the starting point) and a binary operator. - Example (Summing Numbers):
```java
import java.util.Arrays;
import java.util.List;
public class StreamExample {
public static void main(String[] args) {
List numbers \= Arrays.asList(1, 2, 3, 4, 5);
// Sum of numbers int sum = numbers.stream() .reduce(0, (a, b) -> a + b);
System.out.println(sum); // Output: 15
}
}
```
- Example (Concatenating Strings):
```java
import java.util.Arrays;
import java.util.List;
public class StreamExample {
public static void main(String[] args) {
List words \= Arrays.asList("Hello", "World", "Java");
// Concatenate strings with a space String result = words.stream() .reduce("", (a, b) -> a + " " + b);
System.out.println(result.trim()); // Output: Hello World Java
}
}
```
Summary:
- Filtering: Extract elements that satisfy a condition using
filter()
. - Mapping: Transform elements using
map()
. - Reducing: Aggregate elements into a single result using
reduce()
.