Java Steam API Flashcards

1
Q

What is Stream API in Java?

A

Stream API is a set of tools introduced in Java 8 to help process the sequence of elements. You can use this feature to reduce a large amount of boilerplate code and improve an app’s productivity. It also helps to create more readable programs and allows us to bulk process conveniently.

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

What is the difference between Collection and Stream?

A

The main difference between a Collection and a Stream is that Collection contains its elements, but a 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
3
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.

For example, if you have a List of String and you want to convert that to a List of Integer, you can use map() to do so.

Just supply a function to convert String to Integer e.g., parseInt() to map() and it will apply that to all elements of List and give you a List of Integer. In other words, the map can convert one object to another.

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

A predicate function is nothing but a function that takes an Object and returns a boolean. For example, if you have a List of Integer and you want a list of even integers.

In this case, you can use the filter to achieve that. You supply a function to check if a number is even or odd, just like this function, and filter will apply this to stream elements and filter the elements which satisfy the condition and which don’t.

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, if you have a list of the list but you want to combine all elements of lists into just one list. In this case, you can use flatMap() for flattening. At the same time, you can also transform an object like you do use map() function.

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

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

A

Even though both map() and flatMap() can be used to transform one object to another by applying a method to each element.

The main difference is that flatMap() can also flatten the Stream. For example, if you have a list of the list, then you can convert it to a big list by using the flatMap() function.

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

What does the peek() method do? When should you use it?

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
9
Q

What do you mean by saying Stream is lazy?

A

When we say Stream is lazy, we mean that most of the methods are defined on Java .util.stream.Stream class is lazy. i.e. they will not work by just including them on the 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
10
Q

What is a functional interface in Java 8?

A

As the name suggests, a functional interface is an interface that represents a function. Technically, an interface with just one abstract method is called a functional interface.

You can also use @FunctionalInterface to annotated a functional interface. In that case, the compiler will verify if the interface actually contains just one abstract method or not. It’s like the @Override annotation, which prevents you from accidental errors.

Another useful thing to know is that If a method accepts a functional interface, then you can pass a lambda expression to it.

Some examples of the functional interface are Runnable, Callable, Comparator, and Comparable from old API and Supplier, Consumer, and Predicate, etc. from new function API.

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

What is the difference between a normal and functional interface in Java?

A

The normal interface in Java can contain any number of abstract methods, while the functional interface can only contain just one abstract method.

You might be thinking why they are called functional interfaces? Once you know the answer, it might be a little easier for you to remember the concept.

Well, they are called functional interfaces because they wrap a function as an interface. The function is represented by the single abstract method on the interface.

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

What is the difference between the findFirst() and findAny() method?

A

The findFirst() method will return the first element meeting the criterion i.e. Predicate, while the findAny() method will return any element meeting the criterion, very useful while working with a parallel stream. You can further see this article for a working example of the findFirst() method in Java 8.

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

What is a Predicate interface?

A

A Predicate is a functional interface that represents a function, which takes an Object and returns a boolean. It is used in several Stream methods like filter(), which uses Predicate to filter unwanted elements.

here is how a Predicate function looks like:

public boolean test(T object) {
     return boolean; 
}

You can see it just has one test() method which takes an object and returns a boolean. The method is used to test a condition if it passes; it returns true otherwise false.

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

What are Supplier and Consumer Functional interface?

A

The Supplier is a functional interface that returns an object. It’s similar to the factory method or new(), which returns an object.

The Supplier has get() functional method, which doesn’t take any argument and return an object of type T. This means you can use it anytime you need an object.

Since it is a functional interface, you can also use it as the assignment target for a lambda expression or method reference.

A Consumer is also a functional interface in JDK 8, which represents an operation that accepts a single input argument and returns no result.

Unlike other functional interfaces, Consumer is expected to operate via side-effects. The functional method of Consumer is accept(T t), and because it’s a functional interface, you can use it as the assignment target for a lambda expression or method interface in Java 8.

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

Can you convert an array to Stream? How?

A

Yes, you can convert an array to Stream in Java. The Stream class provides a factory method to create a Stream from an array, like Stream .of(T …) which accepts a variable argument, that means you can also pass an array to it as shown in the following example:

String[] languages = {"Java", "Python", "JavaScript"};
Stream numbers = Stream.of(languages);
numbers.forEach(System.out::println);

Output:
Java
Python
JavaScript

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

What is the parallel Stream? How can you get a parallel stream from a List?

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 parts of the Stream and then combine the result.

In short, the parallel Stream can paralyze execution, but, as Cay S. Horstman mentioned in Core Java S.E. for the Impatient, there is significant overhead or parallelism, which only pays off if you are doing bulk data operation.