Java 8 Interview Questions and Answers
The Best LIVE Mock Interview - You should go through before Interview
Test your skills through the online practice test: Java 8 Quiz Online Practice Test
Intermediate / 1 to 5 years experienced level questions & answers
Ques 1. What is lambda expression?
Lambda expression is anonymous function which have set of parameters and a lambda (->) and a function body. You can call it function without name.
Structure of Lambda Expressions:
(Argument List) ->{expression;} or
(Argument List) ->{statements;}
For instance, the Runnable interface is a functional interface, so instead of:
Thread thread = new Thread(new Runnable() {
public void run() {
System.out.println("Hello World!");
}
});
Using Lambda you can simply do the following:
Thread thread = new Thread(() -> System.out.println("Hello World!"));
Functional interfaces are usually annotated with the @FunctionalInterface annotation - which is informative and does not affect the semantics.
Is it helpful?
Add Comment
View Comments
Ques 2. Given a list of employees, sort all the employee on the basis of age by using java 8 APIs only.
You can simply use sort method of list to sort the list of employees.
List<Employee> employeeList = createEmployeeList();
employeeList.sort((e1,e2)->e1.getAge()-e2.getAge());
employeeList.forEach(System.out::println);
Is it helpful?
Add Comment
View Comments
Ques 3. Given the list of employees, find the employee with name 'John' using Java 8 API.
Check the following code:
List<Employee> employeeList = createEmployeeList();
Optional<Employee> e1 = employeeList.stream()
.filter(e->e.getName().equalsIgnoreCase("Mary")).findAny();
if(e1.isPresent())
System.out.println(e1.get());
Is it helpful?
Add Comment
View Comments
Ques 4. Given a list of employee, find maximum age of employee using Java 8 API.
Check the following code:
List<Employee> employeeList = createEmployeeList();
OptionalInt max = employeeList.stream().
mapToInt(Employee::getAge).max();
if(max.isPresent())
System.out.println("Maximum age of Employee: "+max.getAsInt());
Is it helpful?
Add Comment
View Comments
Ques 5. Provide some examples of Intermediate operations.
Example of Intermediate operations are:
- filter(Predicate)
- map(Function)
- flatmap(Function)
- sorted(Comparator)
- distinct()
- limit(long n)
- skip(long n)
Is it helpful?
Add Comment
View Comments
Ques 6. Provide some examples of Terminal operations.
Example of Terminal operations are:
- forEach
- toArray
- reduce
- collect
- min
- max
- count
- anyMatch
- allMatch
- noneMatch
- findFirst
- findAny
Is it helpful?
Add Comment
View Comments
Most helpful rated by users:
- What are new features which got introduced in Java 8?
- What are main advantages of using Java 8?
- Can you explain the syntax of Lambda expression?
- What are functional interfaces?
- What Is a Default Method and When Do We Use It?