What is Stream API?

1. What is the Stream API?
The Stream API is a key component of Java starting from version 8, enabling easy and flexible data collection processing in a functional style. A stream is a sequence of elements that supports sequential and parallel data operations.
2. Benefits of Using Stream API
- Reduces boilerplate code.
- Improves readability and conciseness, especially when working with collections.
- Built-in support for parallel processing (parallel streams).
- Rich set of methods for filtering, sorting, mapping, and collecting data.
3. Key Characteristics of Streams
- Seamless integration with lambda expressions. Stream API is designed with functional operations in mind.
- Streams do not store data. They only move, filter, or transform the provided elements.
- Streams are immutable. They do not modify the source collection.
- Streams cannot be reused. Once consumed, a stream is considered exhausted.
- No random access by index.
- Lazy operations. Execution is deferred until a terminal operation is invoked.
4. Interfaces for Working with Streams
The Stream API consists of the following key interfaces:
- java.util.stream.Stream<T> – for objects.
- DoubleStream, IntStream, LongStream – for primitive types.
5. Creating Streams
Streams can be created in several ways:
1. From a collection:
List<String> words = Arrays.asList("hello", "hola", "hallo", "ciao");
Stream<String> stream = words.stream();
2. Using Stream.of:
Stream<String> stream = Stream.of("hello","hola", "hallo", "ciao");
3. From an array:
String[] words = {"hello", "hola", "hallo", "ciao"};
Stream<String> stream = Stream.of(words);
4. For primitives:
int[] nums = {1, 2, 3, 4, 5};
System.out.println(Arrays.stream(nums).count());
int[] nums = {1, 2, 3, 4, 5};
System.out.println(IntStream.of(nums).count());
5. Using generators and iterators:
Stream<Double> s = Stream.generate(Math::random).limit(5);
Stream<Integer> s = Stream.iterate(1, n -> n * 2).limit(5);
6. Using builder:
Stream<String> s = Stream.<String>builder().add("h").add("e").add("l").add("l").add("o").build();
s.forEach(System.out::print); // "hello"
7. Ranges:
// stream of 1, 2, 3
IntStream s = IntStream.range(1, 4);
// stream of 1, 2, 3, 4
IntStream s = IntStream.rangeClosed(1, 4);
6. Stream API Collection Processing Examples
Example before using Stream API:
List<Student> studentsScore = new ArrayList<>();
for(Student s : students) {
if(s.getScore() > 90.0) {
studentsScore.add(s);
}
}
Collections.sort(studentsScore, new Comparator<>() {
public int compare(Student s1, Student s2) {
return Double.compare(
s1.getScore(), s2.getScore());
}
});
Rewritten using Stream API:
List<Student> studentsScore = students
.stream()
.filter(s -> s.getScore() >= 90.0)
.sorted(Comparator.comparing(Student::getScore))
.collect(Collectors.toList());
A data stream is a data transmission channel. It represents a sequence of objects and operates on a data source like an array or collection. The Stream API defines several stream-related interfaces within the java.util.stream package.

Please log in or register to have a possibility to add comment.