Java -Stream API Introduction with Example
Java 8 introduced the concept of stream, Stream represents a sequence of objects from a source, which supports aggregate operations.
Following are the characteristics of a Stream −
- It takes input from the Collections, Arrays, or I/O channels.
- It never stores the elements.
- Stream supports intermediate operations like filter, map, limit, reduce, find, match, and so on.
- Streams only provide the result as per the pipelined methods.
- Stream operations do the iterations internally over the source elements provided.
Different Operations On Streams-
forEach
The stream has provided a new method ‘forEach’ to iterate each element of the stream
List<Integer> obj = new ArrayList<>();
obj.add(7);
obj.add(12);
obj.add(3);
obj.add(5);
obj.forEach((o) -> System.out.println("Item : " + o));
map
The map method is used to map the items in the collection to other objects according to the Function passed as the argument.
List<String> obj = Arrays.asList("h", "bh", "ca", "d");
List<String> collectUpperCaseLetters = obj.stream().
map(String::toUpperCase).collect(Collectors.toList());
System.out.println(collectUpperCaseLetters);
filter
The ‘filter’ method is used to eliminate elements based on criteria.
List<String> names = Arrays.asList("Sibin", "Rahul", "Jose");
List<String> filteredNames = names.stream().filter(s -> s.startsWith("S")).
collect(Collectors.toList());
System.out.println(filteredNames);
sorted
The sorted method is used to sort the stream.
List<String> names = Arrays.asList("Sibin", "Rahul", "Jose");
List<String> result = names.stream().sorted().collect(Collectors.toList());
System.out.println(result);
reduce
The reduce method is used to reduce the elements of a stream to a single value.
int[] array = {2,4,5,7,2};
Arrays.stream(array).reduce((x,y) -> x+y).ifPresent(s -> System.out.println(s));
Program to reveal the use of Stream
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StreamTest {
public static void main(String[] args) {
// demonstration of forEach method
List<Integer> obj = new ArrayList<>();
obj.add(7);
obj.add(12);
obj.add(3);
obj.add(5);
obj.forEach((o) -> System.out.println(o));
/* End */
// demonstration of map method
List<String> alplalist = Arrays.asList("h", "bh", "ca", "d");
List<String> collectUpperCaseLetters = alplalist.stream().
map(String::toUpperCase).collect(Collectors.toList());
System.out.println(collectUpperCaseLetters);
/* End */
// demonstration of filter method
List<String> names = Arrays.asList("Sibin", "Rahul", "Jose");
List<String> filteredNames = names.stream().
filter(s -> s.startsWith("S")).collect(Collectors.toList());
System.out.println(filteredNames);
/* End */
// demonstration of sorted method
List<String> namelist = Arrays.asList("Sibin", "Rahul", "Jose");
List<String> result = namelist.stream().sorted().
collect(Collectors.toList());
System.out.println(result);
/* End */
// demonstration of reduce method
int[] array = { 2, 4, 5, 7, 2 };
Arrays.stream(array).reduce((x, y) -> x + y).
ifPresent(s -> System.out.println(s));
/* End */
}
}
Output:
7
12
3
5
[H, BH, CA, D]
[Sibin]
[Jose, Rahul, Sibin]
20