Top 10 Java8 Stream API Programming Questions, Frequently asked during Interview - NESTED CODE || TECH FLOAT

Breaking

Post Top Ad

Post Top Ad

Tuesday, 2 January 2024

Top 10 Java8 Stream API Programming Questions, Frequently asked during Interview


 

The Stream API is a new abstraction introduced in Java 8 to process sequences of elements, such as collections, arrays, or I/O channels. It allows developers to perform functional-style operations on the elements, leveraging the power of lambda expressions to write more readable and concise code.


Here you go with top 10 sample programming cases frequently asked during interview

1. Get all the employee names from the employee list - use of map

2. Sort the employee list based on age - use of sorted

3. Get all the phone no from the employee list (where phone is another list) - how to extract a list from list of list - flatmap

4. Group all the employee based on their department

5. Group all the employee based on their department and give count as result

6. Get maximum employee department

7. Find average salary for each department

8. Find max salary for each department

9. Get the second highest salaried employee details in each dept.

10. Filter the employee for each department has salary>3000000


Code : https://gitlab.com/nc-public/java8-streamapi/-/blob/main/com/nc/service/EmployeeService.java?ref_type=heads


package com.nc.service;


import java.util.Comparator;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Map.Entry;

import java.util.Optional;

import java.util.stream.Collectors;


import com.nc.empdb.EmployeeDB;

import com.nc.entity.Employee;


public class EmployeeService {

public static List<Employee> employeeList = EmployeeDB.getEmployeeData();

public static void main(String a[]) {

//1. Get all the employee names from the employee list - use of map

List<String> employeeName = employeeList.stream().map(employee->employee.getName())

.collect(Collectors.toList());

System.out.println(employeeName);

//2. Sort the employee list based on age - use of sorted

List<Employee> sortedEmplListByAge = employeeList.stream().sorted(Comparator.comparing(Employee::getAge))

.collect(Collectors.toList());

System.out.println(sortedEmplListByAge);

//3. Get all the phone no from the employee list (where phone is another list) - how to extract a list from list of list - flatmap

List<String> employeePhoneList =  employeeList.stream().flatMap(employee->employee.getPhone().stream())

.collect(Collectors.toList());

System.out.println(employeePhoneList);

//4. Group all the employee based on their department

Map<String, List<Employee>> collectByDept = employeeList.stream().collect(Collectors.groupingBy(m->m.getDept()));

System.out.println(collectByDept);

//4. Group all the employee based on their department and give count as result

Map<String, Long> collectCountDept = employeeList.stream().collect(Collectors.groupingBy(Employee::getDept, Collectors.counting()));

System.out.println(collectCountDept);

//5. Get maximum employee group

Entry<String, Long> maxDept = employeeList.stream().collect(Collectors.groupingBy(Employee::getDept, Collectors.counting()))

.entrySet().stream().max(Map.Entry.comparingByValue()).get();

System.out.println(maxDept);

//6. based on the dept avg salary

Map<String, Double> collectAvgGroup = employeeList.stream().collect(Collectors.groupingBy(Employee::getDept, Collectors.averagingDouble(Employee::getSalary)));

System.out.println(collectAvgGroup);

//7. based on the dept max salary

Map<String, Optional<Employee>> collectMaxSalGroup = employeeList.stream()

.collect(Collectors.groupingBy(Employee::getDept, Collectors.maxBy(Comparator.comparing(Employee::getSalary))));

System.out.println(collectMaxSalGroup);

//9. Get the second highest salaried employee details in each dept.

HashMap<String, Employee> map = new HashMap<>();

collectByDept.entrySet().stream().forEach(m->{

Optional<Employee> emp = m.getValue().stream().sorted(Comparator.comparing(Employee::getSalary)).skip(1).findFirst();

if(emp.isPresent())

map.put(m.getKey(), emp.get());

else

map.put(m.getKey(), m.getValue().stream().sorted(Comparator.comparing(Employee::getSalary)).findAny().get());

});

System.out.println(map);

//10. Filter the employee for each department has salary>3000000

Map<String, List<Employee>> collectEmpBYSalGrp = employeeList.stream().filter(m->m.getSalary()>3000000).collect(Collectors.groupingBy(Employee::getDept));

System.out.println(collectEmpBYSalGrp);

}


}

 

No comments:

Post a Comment

Post Bottom Ad