Java java microservices Java Virtual Machine
Stream In Java 8
Java 8 provides a new package which is called java.util.stream. Basically, it is used to process collections of objects. It consists of various types of classes, enums, and interfaces to perform operations on different elements. It is like a sequence of objects which supports different types of methods that are pipelined together to produce results. A stream can be used by importing the package java.util.stream.
Features of Java Stream
Below are the features of java stream:
- It does not store any element. It simply takes elements from a source like I/0 channel, data structure, or array, using a pipeline of
different types of operations.
- It is lazily executed any operation and performs any evaluation on code when required.
- In nature, the stream is functional and any operation performed on it does not change its source.
Types of Stream
Basically, there are two types of stream
1. Stream
2. Parallel Stream
Operations On Stream
Below are the operations which we can perform on a stream:
Intermediate Operations:
1). map: It is used to map stream elements to their corresponding result.
example:- List<String> languages = Arrays.asList("java", "angular", "nodejs", "python");
List<String> data = languages.stream().map(String::toUpperCase).collect(Collectors.toList());
System.out.println(data);
output:- [JAVA, ANGULAR, NODEJS, PYTHON]
2). filter: It is used to test elements for a condition and generate a filtered result.
example :- List<Integer> numberList = Arrays.asList(45, 4, 6, 12, 20);
List<Integer> numbers = numberList.stream()
.filter(number -> number % 5 == 0)
.collect(Collectors.toList());
System.out.println(numbers);
output:- [45,20]
3). sorted: It is used to sort stream elements.
example :- List<Integer> numberList = Arrays.asList(-19, -29, 0, 5, 4);
List<Integer> numbers = numberList.stream().sorted().collect(Collectors.toList());
System.out.println(numbers);
output:- [-29, -19, 0, 4, 5]
Terminal Operations:
1). forEach: It can be used to perform an iteration over the stream. Generally, it is used in place of the iterator.
example:- List<String> list = Arrays.asList("java", "python", "nodejs", "angular");
list.stream().forEach(System.out::println);
output:- Java
Python
Nodejs
Angular
2). collect: It is used to return the desired result of the operations performed on the stream.
example :- List<Integer> number = Arrays.asList(8,3,6,5,3);
Set<Integer> square = number.stream().map(x->x*x).collect(Collectors.toSet());
System.out.println(square);
output :- [64,9,36,25]
3). reduce: It is used to perform reduction on elements of the stream.
//for getting the product of all the elements in the given range excluding the rightmost element
example :- int result = IntStream.range(2, 7)
.reduce((num1, num2) -> num1 * num2)
.orElse(-1);
System.out.println(result);
output:- 720
We, at Oodles ERP,provide customERP application development servicesto solve complex business problems of our clients and customers. Our team uses the latest tech stack and agile methodologies to build custom enterprise solutions from the ground-up. Contact us at [email protected] to learn more about our enterprise web app development services.