Prepare Interview

Exams Attended

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Java 8 Interview Questions and Answers

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:

Related differences

Java 7 vs Java 8Java 8 vs Java 9

Related interview subjects

JUnit interview questions and answers - Total 24 questions
Spring Framework interview questions and answers - Total 53 questions
Java Design Patterns interview questions and answers - Total 15 questions
Core Java interview questions and answers - Total 306 questions
Tomcat interview questions and answers - Total 16 questions
Apache Wicket interview questions and answers - Total 26 questions
Java Applet interview questions and answers - Total 29 questions
JAXB interview questions and answers - Total 18 questions
JMS interview questions and answers - Total 64 questions
Log4j interview questions and answers - Total 35 questions
Struts interview questions and answers - Total 84 questions
RMI interview questions and answers - Total 31 questions
Apache Camel interview questions and answers - Total 20 questions
JDBC interview questions and answers - Total 27 questions
Java 11 interview questions and answers - Total 24 questions
JPA interview questions and answers - Total 41 questions
EJB interview questions and answers - Total 80 questions
GWT interview questions and answers - Total 27 questions
Kotlin interview questions and answers - Total 30 questions
Glassfish interview questions and answers - Total 8 questions
Google Gson interview questions and answers - Total 8 questions
JSP interview questions and answers - Total 49 questions
J2EE interview questions and answers - Total 25 questions
Apache Tapestry interview questions and answers - Total 9 questions
Java Swing interview questions and answers - Total 27 questions
Java Mail interview questions and answers - Total 27 questions
Hibernate interview questions and answers - Total 52 questions
JSF interview questions and answers - Total 24 questions
Java 8 interview questions and answers - Total 30 questions
Java 15 interview questions and answers - Total 16 questions
JBoss interview questions and answers - Total 14 questions
Web Services interview questions and answers - Total 10 questions
RichFaces interview questions and answers - Total 26 questions
Servlets interview questions and answers - Total 34 questions
Java Beans interview questions and answers - Total 57 questions
Spring Boot interview questions and answers - Total 50 questions
©2023 WithoutBook