Stream Flashcards Preview

Review Java 8 > Stream > Flashcards

Flashcards in Stream Deck (18)
Loading flashcards...
1

What is the difference between Collection and Stream?

The main difference between a Collection and Stream is that Collection contains their elements, but Stream doesn't.
Stream work on a view where elements are actually stored by Collection or array, but unlike other views, any change made on Stream doesn't reflect on the original Collection.

2

What does the map() function do? why you use it?

The map() function perform map functional operation in Java. This means it can transform one type of object to others by applying a function.

3

What does the filter() method do? when you use it?

The filter method is used to filter elements that satisfy a certain condition that is specified using a Predicate function.

4

Predicate function

A function that takes an Object and returns a boolean

@FunctionalInterface
public interface Predicate

5

What does the flatmap() function do? why you need it?

The flatmap function is an extension of the map function. Apart from transforming one object into another, it can also flatten it.

For example:

String[][] data = new String[][]{{"a", "b"}, {"c", "d"}, {"e", "f"}};

// init a stream
Stream temp = Arrays.stream(data);

// This printout the Array Object
temp.forEach(System.out::println);

// temp stream is closed after previous line, re-init stream
temp = Arrays.stream(data);
Stream stringStream = temp.flatMap(x -> Arrays.stream(x));

// This printout the list of strings
stringStream.forEach(System.out::println);

6

What is difference between flatMap() and map() functions?

Both map and flatMap can be applied to a Stream and they both return a Stream.

The difference is that the map operation produces one output value for each input value, whereas the flatMap operation produces an arbitrary number (zero or more) values for each input value.

7

What Is a Stream?

In simple terms, a stream is an iterator whose role is to accept a set of actions to apply on each of the elements it contains.

8

How Does It Differ from a Collection?

The stream represents a sequence of objects from a source such as a collection, which supports aggregate operations.
They were designed to make collection processing simple and concise. Contrary to the collections, the logic of iteration is implemented inside the stream, so we can use methods like map and flatMap for performing a declarative processing.

Another difference is that the Stream API is fluent and allows pipelining:

int sum = Arrays.stream(new int[]{1, 2, 3})
.filter(i -> i >= 2)
.map(i -> i * 3)
.sum();

And yet another important distinction from collections is that streams are inherently lazily loaded and processed.

9

What is the difference between intermediate and terminal operations on Stream?

The intermediate Stream operation returns another Stream, which means you can further call other methods of Stream class to compose a pipeline.

For example after calling map() or flatMap() you can still call filter() method on Stream.

On the other hand, the terminal operation produces a result other than Streams like a value or a Collection.

Once a terminal method like forEach() or collect() is called, you cannot call any other method of Stream or reuse the Stream.

10

What Is the Difference Between Intermediate and Terminal Operations?

Stream operations are combined into pipelines to process streams. All operations are either intermediate or terminal.

Intermediate operations are those operations that return Stream itself allowing for further operations on a stream.

These operations are always lazy, i.e. they do not process the stream at the call site, an intermediate operation can only process data when there is a terminal operation. Some of the intermediate operations are filter, map and flatMap.

Terminal operations terminate the pipeline and initiate stream processing. The stream is passed through all intermediate operations during terminal operation call. Terminal operations include forEach, reduce, Collect and sum.

To drive this point home, let us look at an example with side effects:


public static void main(String[] args) {
System.out.println("Stream without terminal operation");

Arrays.stream(new int[] { 1, 2, 3 }).map(i -> {
System.out.println("doubling " + i);
return i * 2;
});

System.out.println("Stream with terminal operation");
Arrays.stream(new int[] { 1, 2, 3 }).map(i -> {
System.out.println("doubling " + i);
return i * 2;
}).sum();
}
The output will be as follows:

Stream without terminal operation
Stream with terminal operation
doubling 1
doubling 2
doubling 3
As you can see, the intermediate operations are only triggered when a terminal operation exists.

11

When should you use peek()?

The peek() method of Stream class allows you to see through a Stream pipeline.

You can peek through each step and print meaningful messages on the console. It's generally used for debugging issues related to lambda expression and Stream processing.

12

What is the peek() method?

Stream peek(Consumer super T> action)

This is an intermediate operation.

This method exists mainly to support debugging, where you want to see the elements as they flow past a certain point in a pipeline

13

What do you mean by saying Stream is lazy?

When we say Stream is lazy, we mean that most of the methods defined on Java .util.stream.Stream class is lazy i.e. they will not work by just including them on Stream pipeline.

They only work when you call a terminal method on the Stream and finish as soon as they find the data they are looking for rather than scanning through the whole set of data.

14

3 parts of Stream pipeline

1. A data source
2. Zero or more intermediate operations
3. Zero or one terminal operation

The source provides the elements to the pipeline.

Intermediate operations get elements one-by-one and process them. All intermediate operations are lazy, and, as a result, no operations will have any effect until the pipeline starts to work.

Terminal operations mean the end of the stream lifecycle. Most importantly for our scenario, they initiate the work in the pipeline.

15

Intermediate operations?

Intermediate operations get elements one-by-one and process them. All intermediate operations are lazy, and, as a result, no operations will have any effect until the pipeline starts to work.

16

Terminal operations?

Terminal operations mean the end of the stream lifecycle. Most importantly for our scenario, they initiate the work in the pipeline.

17

What is Stream Pipeline?

A stream pipeline is nothing but combined intermediate and terminal operations

Many stream operations return a stream themselves. This allows operations to be chained to form a larger pipeline.

18

What is the parallel Stream?

A parallel stream can parallel execute stream processing tasks. For example, if you have a parallel stream of 1 million orders and you are looking for orders worth more than 1 million, then you can use a filter to do that.

Unlike sequential Stream, the parallel Stream can launch multiple threads to search for those orders on the different part of Stream and then combine the result.