Convert Array to Streams in Java 8

A new abstract layer was introduced in Java 8 called ‘Stream’.   With the help you Stream, data can be processed in a declarative way which is quite comparable to SQL statements.

Stream basically represents a chain of objects from a particular source that supports aggregate operations. The characteristics of Stream are mentioned below:

  1. Sequence of Elements: A set of elements are provided by the stream. These elements are of a specific type and are presented in a sequential mode. A stream computes elements on the basis of demand. It doesn’t store elements.
  1. Source: Stream takes Arrays, Collections, or I/O resources as its input source.
  1. Aggregate operations: Stream provides support for aggregate operations such as limit, map, filter, reduce, match, find, and may more.
  2. Pipelining: The result of most stream operations is stream itself, so the results can be pipelined. These operations are termed as intermediate operations. The primary purpose of it is to get input, process it, and return the result to the target. The ‘collect( )’ method is an operation that is present at the last part of the pipelining operation that marks the end of the stream.

Generating Streams

There are 2 methods to create a Stream using the Collection interface.

  • stream( ) – It takes collection as its source and returns a sequential stream.
  • parallelStream ( ) – It takes collection as its source and returns a parallel Stream. 

Java Stream is an additional feature that is added in Java 8. It provides various features to structure data.

Program 1: Using Arrays.stream() method

import java.util.*;
import java.util.stream.*;

class Java8ArrayStreamExample
{
	public static void main(String ...s1)
	{
   		 Integer num[]={1,2,3,4,5,6};
    
   		 Stream<Integer> stream = Arrays.stream(num);
   		 System.out.print("Values Before Filter : ");    
   	 	 stream.forEach(each->System.out.print(each+" "));
   		 System.out.println();
   		 System.out.print("Values After Filter : ") ;
  		 Stream<Integer> stream1 = Stream.of(num);
  		 Stream newS=stream1.filter(each->each.intValue()>3);
  		 newS.forEach(each->System.out.print(each+" "));  
		 System.out.println(); 	    
	}
}

Output:

Values Before Filter : 1 2 3 4 5 6
Values After Filter : 4 5 6

Program 2: Using Stream.of() Method by passing an Integer type object

import java.util.*;
import java.util.stream.*;

class Java8StreamOfExample
{
	public static void main(String ...s1)
	{
   	 Integer num[]={1,2,3,4,5,6};
    
   		 Stream<Integer> stream = Stream.of(num);
   		 System.out.print("Values Before Filter : ");    
   	             stream.forEach(eachInt->System.out.print(eachInt+" "));
   	             System.out.println();
   	             System.out.print("Values After Filter : ") ;
  		 Stream<Integer> stream1 = Stream.of(num);
  		 Stream newS=stream1.filter(eachInt->eachInt.intValue()>3);
  		 newS.forEach(eachInt->System.out.print(eachInt+" "));  
		 System.out.println(); 	    
	}
}
 

Output:

Values Before Filter : 1 2 3 4 5 6
Values After Filter : 4 5 6

Program 3 : Using Stream.of() Method by passing an Integer literals

import java.util.*;
import java.util.stream.*;

class Java8StreamOfMoreExamples
{
	public static void main(String ...s1)
	{
   		Stream<Integer> stream = Stream.of(1,2,3,4,5,6);
   	 	System.out.print("Values Before Filter : ");    
   	 	stream.forEach(eachInt->System.out.print(eachInt+" "));
   	 	System.out.println();
   	 	System.out.print("Values After Filter : ") ;
  		Stream<Integer> stream1 = Stream.of(1,2,3,4,5,6);
  		Stream newS=stream1.filter(eachInt->eachInt.intValue()>3);
  		newS.forEach(eachInt->System.out.print(eachInt+" "));  
		System.out.println(); 	    
	}
}

Output:

Values Before Filter : 1 2 3 4 5 6
Values After Filter : 4 5 6

 

[amazon_link asins=’1617291994|9351197433′ template=’ProductCarousel’ store=’progtalk-20|progtalk-21′ marketplace=’US|IN’ link_id=’3a1b3a44-2185-11e8-9e3c-4999bd989566′]

Like this post? Don’t forget to share it!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.