Top 10 Java 8 Stream and Functional Programming Interview Questions Answers

The JDK 8 release has changed the way we write Java. With new functional programming idioms and a powerful Stream API, most of the new Java code is written in functional style. This also means that Stream and Functional programming related questions are increasing on Java interviews. If you are not familiar with Java 8 changes then it's difficult to crack a Java interview nowadays. Though it's not stated anywhere most of the companies now expect Java developers to know at least Java 8, which is also good right? Java 11 is already out and we are looking forward to Java 12 in a couple of months, it makes sense to know at least Java 8 changes.  Since more and more of my readers are asking about Java 8 interview questions to me, I have started a series where I take one or two topics and share 15 to 20 interview questions.

This is the second part of that series, in the first part, I have shared lambda expressions interview questions and today we'll some Stream API and Functional programming interview questions.

All the questions are meant for Java developers and related to whatever functional programming concepts and tools available to Java developers in JDK 8.

These questions are also not very difficult and motivated by the fact that it encourages you to learn essential Java 8 concepts. It's also not a functional heavy and if you are interested in functional programming in general e.g. the like of Scala and Haskel have implemented, I suggest you check Functional programming in Scala book.

Java Functional Programming and Stream Interview Questions

Without any further ado, here is a list of some of the common yet popular functional programming and Stream interview questions for Java programmers. If you have any other Java functional programming question or anything related to JDK 8 Stream API, feel free to share with us and I'll try to find an answer for you.

1. What is the difference between Collection and Stream? (answer)
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 original collection.

2. What does map() function do? why you use it? (answer)
The map() function perform map functional operation in Java. This means it can transform one type of object to other 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 other.

If you want to learn more about these new methods introduce in Java SE 8, I suggest you take a look at The Complete Java Masterclass course on Udemy. One of the most comprehensive courses which cover everything Java developers need to know.

Top 10 Stream and Functional Programming Interview Questions in Java 8

3. What does the filter() method do? when you use it? (answer)
The filter method is used to filter elements which satisfy a certain condition which is specified using Predicate function.

A predicate function is nothing but a function which 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 doesn't.

4. What does flatmap() function do? why you need it? (answer)
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.

5. What is difference between flatMap() and map() functions? (answer)
Even though both map() and flatMap() can be used to transform one object to another by applying a method on 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 flatMap() function.

Btw, if you find trouble understanding these functional programming methods like map, flatMap, filter etc, I suggest you go through From Collections to Streams in Java 8 course on Pluralsight. It's an advanced course but very good.

Functional Programming Interview Questions in Java 8

Btw, you would need a Pluralsight membership to access this course, which costs around $29 monthly or $299 annually (14% discount). I have one and I also suggest all developers have that plan because Pluralsight is like NetFlix for Software developers.

It has more than 5000+ good quality courses on all latest topics. Since we programmers have to learn new things every day, an investment of $299 USD is not bad.

Btw, it also offers a 10-day free trial without any obligation which allows you to watch 200 hours of content. You can watch this course for free by signing for that trial.

6. What is the difference between intermediate and terminal operation on Stream? (answer)
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, terminal operation produces a result other than Stream e.g. 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.

Java 8 Interview Questions Answers

7. What does peek() method does? When should you use it? (answer)
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.

8. What do you mean by saying Stream is lazy? (answer)
When we say Stream is lazy we mean that most of the methods defined on 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.

9. What is a functional interface in Java 8? (answer)
As the name suggests, a functional interface is an interface which 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. You can learn more about those interface in Java What's New in Java 8 on Pluralsight.

Java 8 Stream Interview Questions Answers

10. What is the difference between a normal and functional interface in Java? (answer)
The normal interface in Java can contain any number of the abstract method while the functional interface can only contain just one abstract method.

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

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

11. What is difference between findFirst() and findAny() method? (answer)
The findFirst() method will return the first element meeting the criterion i.e. Predicate while 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 findFirst() method in Java 8.

12. What is a Predicate interface? (answer)
A Predicate is a functional interface which represents a function, which takes an Object and returns a boolean. It is used in several Stream methods e.g. filter() which uses Predicate to filter unwanted elements.

here is how a Predicate function look 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.

13. What are Supplier and Consumer Functional interface? (answer)
The Supplier is a functional interface which returns an object. It's similar to the factory method or new() which return 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.

14. Can you convert an array to Stream? How? (answer)
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);


So, yes, it's possible to convert an array to Stream in Java 8. You can even convert an ArrayList to Stream as explained in that article.

15. What is the parallel Stream? How can you get a parallel stream from a List? (answer)
A parallel stream can parallel execute stream processing task. 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.

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

Top 10 Stream and Functional Programming Interview Questions in Java 8

That's all about some of the common Java 8 Stream and Functional programming concepts based interview questions. Since, Java 8 is now the modern way of writing Java code, more and more companies are looking for Java developers with good Java 8 skills. This means you can expect more Java 8 based question on Java interviews in years to come.

Other Java Interview Questions articles you may like
Java Interview Guide: 200+ Interview Questions and Answers
100+ Data Structure and Algorithms Questions
75+ Programming Interview Questions with Answers
140+ Java Interview Questions from last 5 years
50+ Multithreading and Concurrency Interview Questions
Top 25 Java Collection Interview Questions
5 Free Java 8 and 9 Courses for Developers
10 Free Java courses for Beginners and Experienced Programmers

Thanks for reading this article so far. If you like these Java 8 Functional Programming and Stream API interview questions and my answers then please share with your friends and colleagues. If you have any doubt or feedback then please drop a note.

No comments:

Post a Comment