A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

Java Stream API

Java Stream API

Java Stream API

  • Stream API introduced in java 8. All the classes and interfaces of this stream API is in the java.util.stream package.

  • A stream represents a sequence of elements and it supports different kind of operations to perform computations upon the elements.

  • The Java Stream API it sounds similar to InputStream and OutputStream from Java I/O. But Java 8 streams are a completely different thing.

  • A Stream represents a sequence of objects derived from a source, where combined operations can be performed.

  • We can say streams as operations on data.

  • The use of Stream API is to implement internal iteration.

  • The Java Stream API provides a functional approach to process collections of objects.

  • The InputStream and OutputStream in java I/O are related to streams of bytes.

  • The Java8 Stream API is for processing streams of objects - not bytes.

  • A stream does not store data and it is not a data structure that stores elements.

  • It also never modifies its data source and it simply return a new stream.

  • It Does not support indexed access.

  • Parallelizum of the code with the help of streams are easy and it is faster to use with Streams.

Why Java Stream API ?

  • We use Java streams because it represent a pipeline through which the data will flow and the functions to operate on the data.

  • It makes the code more readable and easily understanable.

  • It may be considered better to declare your intent in code, than to describe how it's done.

  • Streams have a strong affinity with functions.

  • Streams provide the convenient way to apply functions to sequences of objects.

Non Terminal Operations in Java Streams

Filter:-

  • It is used to filter out elements.

  • It returns a new stream that contains elements of the original.

  • It takes a Predicate which is called for each element in the stream.

  • If the element is included in the resulting Stream, the Predicate should return true.

  • It is used to eliminate elements based on a criteria.

  • This operation is intermediate which enables us to call another stream operation on the result.

  • It is used to check stream elements for a particular condition and generate filtered list result.

Filter Program





import java.util.*;
class one
{
		public static void main(String args[])
		{
				ArrayList li = new ArrayList<>();
         
				li.add("Aman");
         
				li.add("Kamal");
         
				li.add("Love");
         
				li.add("Ekloveya");
         
				li.add("Saheb");
         
				//Selecting names containing more than 4 characters
         
				li.stream().filter((String name) -> name.length() > 4).forEach(System.out::println);
		}
}


OutPut:-
Kamal
Ekloveya
Saheb


 

Map:-

  • This method converts an element from one object to another object.

  • It produce a new stream after applying a function to each element of the original stream.

  • The map() is used to transform each object into another type.

  • We use map() to apply functions to an stream.

  • This method performs reduction operation on elements of a stream.

Map Program:-





import java.util.*;
class one
{
		public static void main(String args[])
		{
				ArrayList li = new ArrayList<>();
         
				li.add("Aman");
         
				li.add("Kamal");
         
				li.add("Love");
         
				li.add("Ekloveya");
         
				li.add("Saheb");
         
				// Length of the names
         
				li.stream()
				  .map(String::length)
				  .forEach(System.out::println);
		}
}


OutPut:-
4
5
4
8
5


 

Sorted:-

  • It sorts the stream elements based on the comparator passed that we pass to it.

  • It returns a sorted view of the stream.

sorted Program:-





import java.util.stream.*;
import java.util.*;
class one
{
		public static void main(String args[])
		{
         
				ArrayList li = new ArrayList();

				li.add("Aman");
			
				li.add("Kamal");

				li.add("Sanam");
				
				li.add("Pawan");

				li.stream().sorted()
						   .map(String::toUpperCase)
						   .forEach(System.out::println);
		}
}



OutPut:-
AMAN
KAMAL
PAWAN
SANAM


 

Non Terminal Operations in Java Streams

Reduce:-

  • This method is a terminal operation that can reduce all elements in the stream to a single element.

Reduce Program:-





import java.util.stream.*;
import java.util.*;
class one
{
		public static void main(String args[])
		{
         
				ArrayList li = new ArrayList();

				li.add("Aman");
			
				li.add("Kamal");

				li.add("Sanam");

				Stream stream = li.stream();

				Optional obj = stream.reduce((x, y) ->
				{
						return  y + " + " + x;
				});

				System.out.println(obj.get());
		}
}



OutPut:-
Sanam + Kamal + Aman


 

findFirst():-

  • This method is used to finds the first element from the Stream.

findFirst() Program:-





import java.util.*;
class one
{
		public static void main(String args[])
		{
				Arrays.asList("Aman", "Kamal", "Sanam").stream().findFirst().ifPresent(System.out::println);
		}
}


OutPut:-
Aman


 

Count:-

  • It is used to count the number of items that are present in the stream.

Count Program:-





import java.util.stream.*;
import java.util.*;
class one
{
		public static void main(String args[])
		{
         
				Stream li = Stream.of(10,20,30,40,50);
		
				System.out.println("Count of numbers in the stream are = "+li.count());
		}
}


OutPut:-
Count of numbers in the stream are = 5


 

forEach():-

  • It is used to iterate all the elements of a stream and perform operation on each of elements.

forEach() Program:-





import java.util.stream.*;
import java.util.*;
class one
{
		public static void main(String args[])
		{
         
				Stream li = Stream.of(10,20,30,40,50);
		
				li.forEach(x -> System.out.print(x+" , "));
		}
}


OutPut:-
10 , 20 , 30 , 40 , 50 ,


 

LEARN TUTORIALS

.

.