Java 8 interview questions

Previous
Next

In this post, we will see some more java 8 interview questions. I will try to cover logical questions rather than theory in this particular part. You can find java 8 interview question part 1.

Java 8 interview questions

First 7 questions will be based on below class.


Question 1:
Given a list of employees, you need to filter all the employee whose age is greater than 20 and print the employee names.(Java 8 APIs only)

Answer:

You can simply do it using below statement.

Complete main program for above logic.



Question 2:
Given the list of employees, count number of employees with age 25?

Answer:

You can use combination of filter and count to find this.



Question 3:
Given the list of employees, find the employee with name “Mary”.

Answer:

It is again very simple logic, change the main function in above class as following.



Question 4:
Given a list of employee, find maximum age of employee?

Answer:

It is again very simple logic, change the main function in above class as following.



Question 5:
Given a list of employees, sort all the employee on the basis of age? Use java 8 APIs only

You can simply use sort method of list to sort the list of employees.


Question 6:
Join the all employee names with “,” using java 8?

Answer:

Output:

Employees are: John,Martin,Mary,Stephan,Gary


Question 7:
Given the list of employee, group them by employee name?

Answer:

You can use Collections.groupBy to group list of employees by employee name.

Output:

Name: John ==>[Employee Name: John age: 21, Employee Name: John age: 26]
Name: Martin ==>[Employee Name: Martin age: 19]
Name: Mary ==>[Employee Name: Mary age: 31, Employee Name: Mary age: 18]


Question 8:
Difference between Intermediate and terminal operations in Stream?

Answer:

Java 8 stream supports both intermediate and terminal operation.

Intermediate operations are lazy in nature and do not get executed immediately. Terminal operations are not lazy, they are executed as soon as encountered. Intermediate operation is memorized and is called when terminal operation is called.

All Intermediate operations return stream as it just transforms stream into another and terminal operation don’t.

Example of Intermediate operations are:

filter(Predicate)
map(Function)
flatmap(Function)
sorted(Comparator)
distinct()
limit(long n)
skip(long n)

Example of terminal operations are :

forEach
toArray
reduce
collect
min
max
count
anyMatch
allMatch
noneMatch
findFirst
findAny


Question 9:
Given the list of numbers, remove the duplicate elements from the list.

Answer:

You can simply use stream and then collect it to set using Collections.toSet() method.

You can use distinct as well to avoid duplicates as following.
change main method of above program as below.


Question 10:
Difference between Stream’s findFirst() and findAny()?

findFirst will always return the first element from the stream whereas findAny is allowed to choose any element from the stream.
findFirst has deterministic behavior whereas findAny is nondeterministic behavior.



Question 11:
Given a list of numbers, square them and filter the numbers which are greater 10000 and then find average of them.( Java 8 APIs only)

Answer:

You can use the map function to square the number and then filter to avoid numbers which are less than 10000.We will use average as terminating function in this case.

output:

21846.666666666668


Question 12:
What is use of Optional in Java 8?

Answer:

Java 8 optional can be used to avoid NullPointerException.You can read about the detailed tutorial.



Question 13:

What is predicate function interface?

Answer:

Predicate is single argument function which returns true or false. It has test method which returns boolean.
When we are using filter in above example, we are actually passing Predicate functional interface to it.



Question 14:
What is consumer function interface?

Answer:

Consumer is single argument functional interface which does not return any value.
When we are using foreach in above example, we are actually passing Consumer functional interface to it.



Question 15:
What is supplier function interface?

Answer:

Supplier is function interface which does not take any parameter but returns the value using get method.


That’s all about Java 8 interview questions.

Previous
Next

Join Our News Letter – Stay Updated

Subscribe to Awesome Java Content.




Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog