Java8 Streams
In java 8.0, a stream represents flow of elements (objects) on which operations can be done.
A stream operates on a data source, such as an array or a collection.
A stream, itself, does not provide storage for the data, it simply moves data and performs operations on data such as filtering, sorting etc.
How to get a stream ?
A stream can be obtained in number of ways .. here are the common ways for the same.
Obtaining a stream for a collection
Beginning with JDK 8, Collection interface has been expanded to add two new methods that obtain a stream from a collection.
stream()
Its default implementation returns a sequential stream.
parallelStream()
Its default implementation returns a parallel stream. If parallel stream is not possible, it may return a sequential stream.
Here is an example that obtains a stream from an arraylist and displays the elements.
import java.util.ArrayList; import java.util.List; import java.util.stream.Stream; public class Stream1 { public static void main(String[] args) { List lst = new ArrayList(); for(int i=0;i<5;i++){ lst.add(i); } Stream stream = lst.stream(); stream.forEach(System.out::println); } }
Running the program will print elements of the stream as :
0
1
2
3
4
Obtaining a stream for an array using Arrays.stream()
Beginning JDK 8, a static stream() method was added to the Arrays class.
This method returns a sequential stream to elements in an array.
For example,
Arrays.stream(array) returns a stream to the array.
Here is the complete code :
import java.util.Arrays; import java.util.stream.Stream; public class Stream3 { public static void main(String[] args) { //Create array of Integers Integer arr[] = {10,20,30,40,50}; //Create stream from the array using Stream.of() Stream stream = Arrays.stream(arr); //Display element of the stream stream.forEach(System.out::println); } }
Running the program will display the elements of the stream:
10
20
30
40
50
Using Stream.of() method to create a stream
Stream.of( ) method takes some values or an array as parameter and constructs a Stream object.
For example, below statement creates a stream using some values..
Stream<Integer> numberStream = Stream.of(1,2,3,4,5);
If we have an array of Integers called arr, we can obtain a stream as :
Stream<Integer> numberStream = Stream.of(arr);
Here is a complete example that creates a stream from an array and then displays the elements.
import java.util.stream.Stream; public class Stream2 { public static void main(String[] args) { //Create array of Integers Integer arr[] = {10,20,30,40,50}; //Create stream from the array using Stream.of() Stream stream = Stream.of(arr); //Display element of the stream stream.forEach(System.out::println); } }
Running the program will display the elements of the stream:
10
20
30
40
50
Streams can be obtained in a variety of other ways.
For example, many stream operations return a new stream. For example, Stream class’s generate() method executes an expression repeatedly and returns a stream containing the returned values.
A stream to an I/O source can be obtained by calling lines() on a BufferedReader.
Once the stream is obtained in using any of these approaches, we can perform the operations on it. The next article will be regarding the operations that can be performed on streams.
Stream Operations
Please refer following post for stream operations:
Stream Operations explained with examples
You may also like reading
© 2015 – 2018, https:. All rights reserved. On republishing this post, you must provide link to original post
[…] Java8 Streams […]