Java 8 – Stream Api

Stream Apis is used to process collections of objects, a stream is a sequence of objects that supports many methods which can be pipelined to get the desired result.

Difference between Java 8 Stream and same operations perfomed in old java sequential way.

This will help you understand the need of Java 8 stream and its usefulness.

Before Java 8 streamsIn Java 8 stream way
// Sum of number of a list greater than 20
private static int sumNumbers(List<Integer> list) {
Iterator<Integer> it = list.iterator();
int sum = 0; while (it.hasNext()) {
int num = it.next();
if (num > 20) {
sum += num;
}
}
return sum;
}
// Sum of number of a list greater than 20 private static int sumUsingStream(List<Integer> list) {
return list.stream().
filter(i -> i > 20).mapToInt(i -> i).sum();
}
This is also called external iteration because client program is handling the algorithm to iterate over the list.Java Stream API to implement internal iteration, that is better because java framework is in control of the iteration
The program is sequential in nature, there is no way we can do this in parallel easily and a lot of code to be written for a simple task.Internal iteration provides several features such as sequential and parallel execution, filtering based on the given criteria, mapping etc and at the same time very less lines of code.

Converting Java Stream to Collection or Array

  1. We can use java Stream collect() method to get List, Map or Set from stream.
    Example:
    List<Integer> intList = intStream.collect(Collectors.toList());
    Map<Integer,Integer> intMap = intStream.collect(Collectors.toMap(i -> i, i -> i+10));
  2. We can use stream toArray() method to create an array from the stream.
    Example:
    Streamt<Integert> intStream = Stream.of(1,2,3,4);
    Integer[] intArray = intStream.toArray(Integer[]::new);
  3. We can use Collection stream() to create sequential stream and parallelStream() to create parallel stream.
    Example:
    //sequential stream
    Stream<Integer> sequentialStream = myList.stream();
    //parallel stream
    Stream<Integer> parallelStream = myList.parallelStream();

Java Stream Intermediate Operations

  1. Stream filter() example: We can use filter() method to test stream elements for a condition and generate filtered list.
    List<Integer> myList = new ArrayList<>();
    for(int i=0; i<100; i++)
    myList.add(i);
    Stream<Integer> sequentialStream = myList.stream();
    Stream<Integer> highNums = sequentialStream.filter(p -> p > 90); //filter numbers greater than 90
    System.out.print(“High Nums greater than 90=”);
    highNums.forEach(p -> System.out.print(p+” “)); //prints “High Nums greater than 90=91 92 93 94 95 96 97 98 99 ”
  2. Stream map() example: We can use map() to apply functions to an stream. Let’s see how we can use it to apply upper case function to a list of Strings.
    Stream<String> names = Stream.of(“adc”, “f”, “kl”);
    System.out.println(names.map(s -> { return s.toUpperCase(); }).collect(Collectors.toList())); //prints [ADC, F, KL]
  3. Stream sorted() example: We can use sorted() to sort the stream elements by passing Comparator argument.
    Stream<String> names2 = Stream.of(“aBc”, “d”, “ef”, “123456”);
    List<String> reverseSorted = names2.sorted(Comparator.reverseOrder()).collect(Collectors.toList()); System.out.println(reverseSorted); // [ef, d, aBc, 123456]
    Stream<String> names3 = Stream.of(“aBc”, “d”, “ef”, “123456”);
    List<String> naturalSorted = names3.sorted().collect(Collectors.toList()); System.out.println(naturalSorted); //[123456, aBc, d, ef]
  4. Stream flatMap() example: We can use flatMap() to create a stream from the stream of list. Let’s see a simple example to clear this doubt.
    Stream<List<String>> namesOriginalList = Stream.of( Arrays.asList(“Pankaj”), Arrays.asList(“David”, “Lisa”), Arrays.asList(“Amit”)); //flat the stream from List<String> to String stream
    Stream<String> flatStream = namesOriginalList.flatMap(strList -> strList.stream()); flatStream.forEach(System.out::println);

Java Stream Terminal Operations

  1. Stream reduce() example: We can use reduce() to perform a reduction on the elements of the stream, using an associative accumulation function, and return an Optional. Let’s see how we can use it multiply the integers in a stream. Stream<Integer> numbers = Stream.of(1,2,3,4,5); Optional<Integer> intOptional = numbers.reduce((i,j) -> {return i*j;}); if(intOptional.isPresent()) System.out.println(“Multiplication = “+intOptional.get()); //120
  2. Stream count() example: We can use this terminal operation to count the number of items in the stream. Stream<Integer> numbers1 = Stream.of(1,2,3,4,5); System.out.println(“Number of elements in stream=”+numbers1.count()); //5
  3. Stream forEach() example: This can be used for iterating over the stream. We can use this in place of iterator. Let’s see how to use it for printing all the elements of the stream. Stream<Integer> numbers2 = Stream.of(1,2,3,4,5); numbers2.forEach(i -> System.out.print(i+”,”)); //1,2,3,4,5,
  4. Stream match() examples: Let’s see some of the examples for matching methods in Stream API. Stream<Integer> numbers3 = Stream.of(1,2,3,4,5); System.out.println(“Stream contains 4? “+numbers3.anyMatch(i -> i==4)); //Stream contains 4? true Stream<Integer> numbers4 = Stream.of(1,2,3,4,5); System.out.println(“Stream contains all elements less than 10? “+ numbers4.allMatch(i -> i<10)); //Stream contains all elements less than 10? true Stream<Integer> numbers5 = Stream.of(1,2,3,4,5); System.out.println(“Stream doesn’t contain 10? “+numbers5.noneMatch(i -> i==10)); //Stream doesn’t contain 10? true
  5. Stream findFirst() example: This is a short circuiting terminal operation, let’s see how we can use it to find the first string from a stream starting with D. Stream<String> names4 = Stream.of(“Pankaj”,”Amit”,”David”, “Lisa”); Optional<String> firstNameWithD = names4.filter(i -> i.startsWith(“D”)).findFirst(); if(firstNameWithD.isPresent()){ System.out.println(“First Name starting with D=”+firstNameWithD.get()); //David }

Summary

In this article we learnt about Java 8 Stream API, converting stream to various java collections , stream apis various other methods and its examples. We suggest you try it out.
Hope you liked the article !


Leave a Comment