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 a 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, particularly Investment banks like Barclays, Citi, and Goldman Sachs, now expect Java developers to know at least Java 8, which is also good, right? Java 19 is already out, and we are looking forward to Java 21 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 see 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 are available to Java developers in JDK 8.
To be honest, even though these are frequently asked Java 8 Interview questions, they are not at all very difficult and just focus on essential Java 8 concepts, rather than their complex implementation.
They are also not functional heavy, but, if you are interested in true functional programming something like Scala and Haskell has implemented, I suggest you check these best Scala Programming courses for Java programmers.
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.
If you want to learn more about these new methods introduced in Java S.E. 8, I suggest you take a look at these best Java Programming courses from Udemy and Coursera. One of the most comprehensive courses which cover everything Java developers need to know.
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.
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.
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.
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.
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.
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.
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 interfaces in Java in these Java 8 online courses.
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.
12. What is a Predicate interface? (answer)
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 see 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 are available to Java developers in JDK 8.
To be honest, even though these are frequently asked Java 8 Interview questions, they are not at all very difficult and just focus on essential Java 8 concepts, rather than their complex implementation.
They are also not functional heavy, but, if you are interested in true functional programming something like Scala and Haskell has implemented, I suggest you check these best Scala Programming courses for Java programmers.
10 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 questions 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 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.2. What does the 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 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.
If you want to learn more about these new methods introduced in Java S.E. 8, I suggest you take a look at these best Java Programming courses from Udemy and Coursera. One of the most comprehensive courses which cover everything Java developers need to know.
3. What does the filter() method do? when you use it? (answer)
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.
4. What does the 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 the 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 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.
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.
6. What is the difference between intermediate and terminal operations 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, 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.
7. What does the peek() method do? 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 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.
9. What is a functional interface in Java 8? (answer)
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. You can learn more about those interfaces in Java in these Java 8 online courses.
10. What is the difference between a normal and functional interface in Java? (answer)
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.
11. What is the difference between the findFirst() and findAny() method? (answer)
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.12. What is a Predicate interface? (answer)
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:
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)
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.
13. What are Supplier and Consumer Functional interface? (answer)
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.
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)
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);
numbers.forEach(System.out::println);
Output:
Java
Python
JavaScript
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 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.
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 questions on Java interviews in years to come.
Other Java Interview Questions articles you may like
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 them with your friends and colleagues. If you have any doubts or feedback, then please drop a note.
P. S. - If you are new to Java and just starting with programming and coding, then I also recommend you go through this list of best Core Java courses to learn the Core Java basics before deep diving on Stream and Functional Programming. This will help you to understand Java better.
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.
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 questions on Java interviews in years to come.
Other Java Interview Questions articles you may like
- 130+ Core Java Interview Questions with Answers
- 100+ Data Structure and Algorithms Questions
- 75+ Programming Interview Questions with Answers
- 10 Courses to Prepare for Coding Interviews
- 140+ Java Interview Questions from last 5 years
- 10 Courses to learn Java in depth
- 50+ Multithreading and Concurrency Interview Questions
- Top 5 Courses to learn Concurrency in Java
- Top 25 Java Collection Interview Questions
- 5 Free Java 8 and 9 Courses for Developers
- 25 Software Design Interview Questions for Programmers
- 100+ Coding Problems from Interviews
- 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 them with your friends and colleagues. If you have any doubts or feedback, then please drop a note.
P. S. - If you are new to Java and just starting with programming and coding, then I also recommend you go through this list of best Core Java courses to learn the Core Java basics before deep diving on Stream and Functional Programming. This will help you to understand Java better.
Hi mate, if you are experienced and trying for switching job then I can refer you in my organization. you can forward your resume to srinath.chinthakindi@gmail.com
ReplyDeleteHi I am looking out for change actively. Please reach out to me below. https://www.linkedin.com/in/saravanamuthu-natarajan/
Deletethanks
ReplyDelete