Skip to content

Ruptam/java-8-stream-api-coding-interview-question

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

Java 8 stream Interview Questions & Answers

Click ⭐if you like the project. Pull Requests are highly appreciated. Connect me @Ruptam for technical content.

Table of Contents

No. Questions
1 Find list of employees whose name starts with alphabet A
2 Group The employees By Department Names
3 Find the total count of employees using stream
4 Find the max age of employees
5 Find all department names
6 Find the count of employee in each department
7 Find the list of employees whose age is less than 30
8 Find the list of employees whose age is in between 26 and 31
9 Find the average age of male and female employee
10 Find the department who is having maximum number of employee
11 Find the Employee who stays in Delhi and sort them by their names
12 Find the average salary in all departments
13 Find the highest salary in each department
14 Find the list of employee and sort them by their salary
15 Find the employee who has second highest salary
  1. Find list of employees whose name starts with alphabet A

    List<Employee> aEmp = empList.stream()
      	.filter(emp -> emp.getName().startsWith("A"))
      				.collect(Collectors.toList());

⬆ Back to Top

  1. Group The employees By Department Names

       Map<String, List<Employee>> deptMap = empList.stream()
     .collect(Collectors.groupingBy(emp -> emp.getDepartmentName()));

⬆ Back to Top

  1. Find the total count of employees using stream

       long empCount = empList.stream().count();

⬆ Back to Top

  1. Find the max age of employees

       int maxAge = empList.stream().mapToInt(emp -> emp.getAge()).max().getAsInt();

⬆ Back to Top

  1. Find all department names

       List<String> deptNamesList = empList.stream()
     .map(emp -> emp.getDepartmentName()).collect(Collectors.toList());

⬆ Back to Top

  1. Find the count of employee in each department

       Map<String, Long> deptCountMap = empList.stream().collect(Collectors.groupingBy(Employee::getDepartmentName, Collectors.counting()));

⬆ Back to Top

  1. Find the list of employees whose age is less than 30

       List<Employee> ageList = empList.stream().filter(emp -> emp.getAge() < 30).collect(Collectors.toList());

⬆ Back to Top

  1. Find the list of employees whose age is in between 26 and 31

       List<Employee> ageBetween26And30 = employees.stream().filter(emp -> emp.getAge() < 30 && emp.getAge() > 26).collect(Collectors.toList());

⬆ Back to Top

  1. Find the average age of male and female employee

       Map<String, Double> avgAgeMap = empList.stream().collect(Collectors.groupingBy(Employee::getGender, Collectors.averagingInt(Employee::getAge)));

⬆ Back to Top

  1. Find the department who is having maximum number of employee

   Map.Entry<String, Long> deptMaxCount = empList.stream().collect(Collectors.groupingBy(
 			Employee::getDepartmentName, Collectors.counting()))
 			.entrySet().stream().max(Map.Entry.comparingByValue()).get();

⬆ Back to Top

  1. Find the Employee who stays in Delhi and sort them by their names

   List<Employee> employees = empList.stream().filter(emp -> emp.getAddress().equals("Delhi")).sorted(Comparator.comparing(Employee::getName)).collect(Collectors.toList());

⬆ Back to Top

  1. Find the average salary in all departments

   Map<String, Double> deptAvgSalary = empList.stream().collect(Collectors.groupingBy(Employee::getDepartmentName, Collectors.averagingDouble(Employee::getSalary)));

⬆ Back to Top

  1. Find the highest salary in each department

   Map<String, Optional<Employee>> highestSalForEachDedpt = employees.stream().collect(Collectors.groupingBy(Employee::getDepartNames, Collectors.minBy(Comparator.comparing(Employee::getSalary))));

⬆ Back to Top

  1. Find the list of employee and sort them by their salary

   List<Employee> emps = empList.stream().sorted(Comparator.comparing(Employee::getSalary)).collect(Collectors.toList());

⬆ Back to Top

  1. Find the employee who has second lowest salary

   Employee emp = empList.stream().sorted(Comparator.comparing(Employee::getSalary)).skip(1).findFirst().get();

⬆ Back to Top

Good luck with your interview 😊


About

Conding questions of stream API for interview

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages