Link Search Menu Expand Document

TOP-5 Java Streams Interview Questions

In this article, we will be discussing some important interview questions related to Java streams.

1. What are the advantages of streams?

Answer:

Streams have the following advantages: 1) They help to perform bulk operations on collections with very little code. Without streams, developers would need to write a lot of code consisting of several loops 2) They operate lazily. So, when a number of stream operations are chained, an operation is performed only when the previous operation is completed. 3) They can operate on the elements in the stream in parallel. This makes them efficient on multi-core systems.

2. Explain how you can convert a stream to a collection and vice versa

Answer:

All the collection interfaces and classes have a method called stream. This can be used to obtain a stream on the underlying collection. Consider the following code:

List input = Arrays.asList(10,20,30);
Stream inputStream = input.stream();

Line 1 defines an Integer List called input and Line 2 invokes the stream method on it to obtain a stream corresponding to the input.

The Stream interface has a method called collect. This can be used to convert a stream to a collection. Consider the following code:

Stream colours = Stream.of("red","blue","white");
List coloursList = colours.collect(Collectors.toList());

Line 1 creates a String Stream called colours and Line 2 invokes the collect method on colours. It passes as a parameter a Collector that can be used to convert a Stream to a List

3. Explain the Stream map operation with a code sample

Answer:

The Stream map operation can be used to convert a Stream of one type to a different type. It accepts as a parameter a Function (which in an in-built functional interface) and applies the Function to every element in the Stream.

Suppose we have a Person class as follows:

public class Person {
String name;
int age;

//constructor, getters and setters
}

The Person class has fields corresponding to name and address. And suppose we have a List of Person objects as follows:

Person p1 = new Person("Bill",34);
Person p2 = new Person("Garry",25);
Person p3 = new Person("Anna",29);

List personList = Arrays.asList(p1,p2,p3);

Suppose we want a List consisting of only the Person names. We can use the map method as follows:

Stream personNames = personList.stream().map(person -> person.getName());

This code first invokes the stream operation on personList. This returns a stream. It then invokes the map operation on the stream. A lambda expression is passed which accepts a person object and returns the name field. This is applied to every element in the stream. Thus, it returns a stream consisting of only the Person names

4. What is the difference between intermediate and terminal stream operations

Answer:

Stream operations can be categorized into intermediate and terminal operations. The following are the differences between them: 1) Intermediate stream operations operate on streams and produce a stream result. Terminal stream operations operate on streams but produce a non-stream result 2) Intermediate stream operations can be chained, terminal stream operations cannot be chained 3) Some examples of intermediate operations are filter, map. Some examples of terminal operations are count, collect.

5. Which stream operation can be used to sort a stream?

Answer:

The Stream interface has a method called sorted. This sorts the stream on which it is invoked in its natural order and returns the sorted stream. Consider the following code:

Stream input = Stream.of(67,31,89,76,45);
Stream output = input.sorted();

Line 1 defines an Integer stream called input and Line 2 invokes the sorted method on input. Since input is an Integer stream, the sorted method sorts it in ascending order.

Other useful articles:


Back to top

© , Java Interviews — All Rights Reserved - Terms of Use - Privacy Policy