Stream Flashcards

1
Q

What is the difference between Collection and Stream?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Predicate function

A

A function that takes an Object and returns a boolean

@FunctionalInterface
public interface Predicate

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A

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);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What Is a Stream?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How Does It Differ from a Collection?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What Is the Difference Between Intermediate and Terminal Operations?

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When should you use peek()?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the peek() method?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What do you mean by saying Stream is lazy?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

3 parts of Stream pipeline

A
  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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Intermediate operations?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Terminal operations?

A

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

17
Q

What is Stream Pipeline?

A

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
Q

What is the parallel Stream?

A

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.