Junior — Middle
Have you used the Stream API when working with collections in your projects?
sobes.tech AI
Answer from AI
Yes, the Stream API in Java is used for convenient and efficient processing of collections. It allows writing declarative code for filtering, transforming, sorting, and aggregating data.
Example of using the Stream API for filtering and printing a list of numbers:
import java.util.Arrays;
import java.util.List;
public class StreamExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
numbers.stream()
.filter(n -> n % 2 == 0) // keep only even numbers
.forEach(System.out::println); // print to console
}
}
This approach simplifies working with collections and makes the code more readable.