Middle+
Tell us about Stream API — what it is and how you use it.
sobes.tech AI
Answer from AI
Stream API in Java is a powerful tool for working with collections and data sequences, introduced in Java 8. It allows writing declarative code for data processing using chains of operations such as filtering, transforming, sorting, and aggregating.
Main features:
- Lazy evaluation: operations are performed only when necessary.
- Readability and compactness of code.
- Ability to process data in parallel.
Example of usage:
import java.util.*;
import java.util.stream.*;
List<String> names = Arrays.asList("Anna", "Bob", "Charlie", "David");
List<String> filtered = names.stream()
.filter(name -> name.length() > 3)
.map(String::toUpperCase)
.sorted()
.collect(Collectors.toList());
System.out.println(filtered); // [ANNA, CHARLIE, DAVID]
Using Stream API for convenient data processing, such as filtering, transforming, and aggregating collections, makes the code more expressive and maintainable.