Popular Feed

Streams in Java 8

A stream does not store data and, in that sense, is not a data structure. It also never modifies the underlying data source.

java8 streams

private static List<Employee> empList = Arrays.asList(arrayOfEmps);
empList.stream();

A stream represents a sequence of elements and supports different kind of operations to perform computations upon those elements:

List<String> myList =
    Arrays.asList("a1", "a2", "b1", "c2", "c1");

myList
    .stream()
    .filter(s -> s.startsWith("c"))
    .map(String::toUpperCase)
    .sorted()
    .forEach(System.out::println);

How to work with Stream in Java

As we have seen in the above example, the working of stream can be explained in three stages:
1. Create a stream

2. Perform intermediate operations on the initial stream to transform it into another stream and so on on further intermediate operations. In the above example, the filter() operation is intermediate operation, there can be more than one intermediate operations.

3. Perform terminal operation on the final stream to get the result. In the above example, the count() operation is terminal operation.


Intermediate Operations:

  1. map: The map method is used to returns a stream consisting of the results of applying the given function to the elements of this stream.
    List number = Arrays.asList(2,3,4,5);
    List square = number.stream().map(x->x*x).collect(Collectors.toList());
  2. filter: The filter method is used to select elements as per the Predicate passed as argument.
    List names = Arrays.asList("Reflection","Collection","Stream");
    List result = names.stream().filter(s->s.startsWith("S")).collect(Collectors.toList());
  3. sorted: The sorted method is used to sort the stream.
    List names = Arrays.asList("Reflection","Collection","Stream");
    List result = names.stream().sorted().collect(Collectors.toList());
Terminal Operations:

  1. collect: The collect method is used to return the result of the intermediate operations performed on the stream.
    List number = Arrays.asList(2,3,4,5,3);
    Set square = number.stream().map(x->x*x).collect(Collectors.toSet());
  2. forEach: The forEach method is used to iterate through every element of the stream.
    List number = Arrays.asList(2,3,4,5);
    number.stream().map(x->x*x).forEach(y->System.out.println(y));
  3. reduce: The reduce method is used to reduce the elements of a stream to a single value.
    The reduce method takes a BinaryOperator as a parameter.

    List number = Arrays.asList(2,3,4,5);
    int even = number.stream().filter(x->x%2==0).reduce(0,(ans,i)-> ans+i);

Java Stream Features

1. Stream does not store the elements. it simply performs the aggregate operations(such as filter() and count() that we have seen in the above example) to get the desired stream of data.

2. The aggregate operations that we perform on the collection, array or any other data source do not change the data of the source, they simply return a new stream. For example the code we have seen above is filtering the strings with length less than 6 using the stream operations but it didn’t change the elements of the list.

3. All the stream operations are lazy in nature which means they are not executed until they are needed. For example, if we want to display only the first 2 elements of a list using stream, the stream operation would stop at the end of second iteration after displaying the second element of list.

Parallel Streams java 8


No comments: