Stream API

Stream API introduced in Java 8 is used to process collections of objects. A stream is a sequence of objects that supports various methods to a pipeline to produce the desired result.

Features

  • One cannot consider the stream as a data structure. Instead, it accepts input from the Collections, Arrays, or I/O channels.
  • It doesn’t manipulate the original data structure; it only provides the result generated by pipelined methods.
  • A stream consists of a set of elements in a sequential manner of a specific type.
  • It supports aggregate operations. For instance, filter, map, limit, reduce, find, match, and so on.
  • Unlike Collections, the iterations are performed internally by stream operations over the source elements provided. However, in collections, explicit iteration is mandatory.
  • It does not store elements. 
  • Stream evaluates the code only when necessary, as it is lazy.
  • A stream mainly consists of three things :
    • a source
    • zero or more intermediate methods combined (pipelined)
    • a terminal method that processes the objects obtained from the source.
  • To pipeline the result, most of the stream operations return the stream itself. These operations are intermediate. The function of intermediate operations is to take input, process them, and return output to the target.
  • A terminal operation is the collect() method present at the end of the pipelining operation to mark the end of the stream.
  • Collectors can combine the result of processing on the elements of a stream. Collectors can return a list or a string.
  • However, the operations that are performed on a stream does not modify the source of the stream. 

Operations Performed on Stream

  • Intermediate Operations
    • map
      • The map method returns a stream consisting of the results of applying the given function to the elements of this stream.
      • list.stream().map(function).collect(Collectors.toList());
    • filter
      • The filter method select the elements as per the Predicate passed as argument.
      • list.stream().filter(predicate).collect(Collectors.toList())
    • sorted
      • The sorted method sort the stream.
      • list.stream().sorted().collect(Collectors.toList());
  • Terminal Operations
    • collect
      • The collect method returns the result of the intermediate operations performed on the stream.
      • list.stream().map(function).collect(Collectors.toSet());
    • forEach
      • The forEach method iterates through every element of the stream.
      • list.stream().map(function).forEach(action);
    • reduce
      • The reduce method reduce the elements of a stream to a single value. It takes a BinaryOperator as a parameter.
      • list.stream().filter(predicate).reduce(binaryOperator);

class StreamAPIExample {
	public static void main(String args[]) {
		List<Integer> numbers = Arrays.asList(2,3,4,5);
		
		List<String> names = Arrays.asList("System", "Operator", "StreamAPI");
		
		List<Integer> square = numbers.stream().map(x -> x*x).collect(Collectors.toList());
		
		List<String> result = names.stream().filter(s -> s.startsWith("S")).collect(Collectors.toList());
		
		List<String> show = names.stream().sorted().collect(Collectors.toList());
		
		Set<Integer> squareSet = numbers.stream().map(x -> x*x).collect(Collectors.toSet());
		
		numbers.stream().map(x -> x*x).forEach(y -> System.out.println(y));
		
		int even = numbers.stream().filter(x -> x%2 == 0).reduce(0, (ans, i) -> ans + i);
	}
}
Scroll to Top