Java 8 Interviews Programs
Java 8 Interviews Programs

Java 8 Interviews Programs

This Java program processes a list of `Employee` objects to answer various queries about the employees’ attributes and organizational structure. Here’s a breakdown:

Main Function:

The `main` method initializes a list of `Employee` objects with various attributes like name, age, gender, department, year of joining, and salary.

package com.javainfotech.example;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.DoubleSummaryStatistics;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class EmployeeTest {

	public static void main(String[] args) {
		List<Employee> employeeList = new ArrayList<Employee>();
		employeeList.add(new Employee(111, "Jiya Brein", 32, "Female", "HR", 2011, 25000.0));
		employeeList.add(new Employee(122, "Paul Niksui", 25, "Male", "Sales And Marketing", 2015, 13500.0));
		employeeList.add(new Employee(133, "Martin Theron", 29, "Male", "Infrastructure", 2012, 18000.0));
		employeeList.add(new Employee(144, "Murali Gowda", 28, "Male", "Product Development", 2014, 32500.0));
		employeeList.add(new Employee(155, "Nima Roy", 27, "Female", "HR", 2013, 22700.0));
		employeeList.add(new Employee(166, "Iqbal Hussain", 43, "Male", "Security And Transport", 2016, 10500.0));
		employeeList.add(new Employee(177, "Manu Sharma", 35, "Male", "Account And Finance", 2010, 27000.0));
		employeeList.add(new Employee(188, "Wang Liu", 31, "Male", "Product Development", 2015, 34500.0));
		employeeList.add(new Employee(199, "Amelia Zoe", 24, "Female", "Sales And Marketing", 2016, 11500.0));
		employeeList.add(new Employee(200, "Jaden Dough", 38, "Male", "Security And Transport", 2015, 11000.5));
		employeeList.add(new Employee(211, "Jasna Kaur", 27, "Female", "Infrastructure", 2014, 15700.0));
		employeeList.add(new Employee(222, "Nitin Joshi", 25, "Male", "Product Development", 2016, 28200.0));
		employeeList.add(new Employee(233, "Jyothi Reddy", 27, "Female", "Account And Finance", 2013, 21300.0));
		employeeList.add(new Employee(244, "Nicolus Den", 24, "Male", "Sales And Marketing", 2017, 10700.5));
		employeeList.add(new Employee(255, "Ali Baig", 23, "Male", "Infrastructure", 2018, 12700.0));
		employeeList.add(new Employee(266, "Sanvi Pandey", 26, "Female", "Product Development", 2015, 28900.0));
		employeeList.add(new Employee(277, "Anuj Chettiar", 31, "Male", "Product Development", 2012, 35700.0));
		employeeList.add(new Employee(111, "Jiya Brein", 32, "Female", "HR", 2011, 25000.0));
		employeeList.add(new Employee(122, "Paul Niksui", 25, "Male", "Sales And Marketing", 2015, 13500.0));
		employeeList.add(new Employee(133, "Martin Theron", 29, "Male", "Infrastructure", 2012, 18000.0));
		employeeList.add(new Employee(144, "Murali Gowda", 28, "Male", "Product Development", 2014, 32500.0));
		employeeList.add(new Employee(155, "Nima Roy", 27, "Female", "HR", 2013, 22700.0));
		employeeList.add(new Employee(166, "Iqbal Hussain", 43, "Male", "Security And Transport", 2016, 10500.0));
		employeeList.add(new Employee(177, "Manu Sharma", 35, "Male", "Account And Finance", 2010, 27000.0));
		employeeList.add(new Employee(188, "Wang Liu", 31, "Male", "Product Development", 2015, 34500.0));
		employeeList.add(new Employee(199, "Amelia Zoe", 24, "Female", "Sales And Marketing", 2016, 11500.0));
		employeeList.add(new Employee(200, "Jaden Dough", 38, "Male", "Security And Transport", 2015, 11000.5));
		employeeList.add(new Employee(211, "Jasna Kaur", 27, "Female", "Infrastructure", 2014, 15700.0));
		employeeList.add(new Employee(222, "Nitin Joshi", 25, "Male", "Product Development", 2016, 28200.0));
		employeeList.add(new Employee(233, "Jyothi Reddy", 27, "Female", "Account And Finance", 2013, 21300.0));
		employeeList.add(new Employee(244, "Nicolus Den", 24, "Male", "Sales And Marketing", 2017, 10700.5));
		employeeList.add(new Employee(255, "Ali Baig", 23, "Male", "Infrastructure", 2018, 12700.0));
		employeeList.add(new Employee(266, "Sanvi Pandey", 26, "Female", "Product Development", 2015, 28900.0));
		employeeList.add(new Employee(277, "Anuj Chettiar", 31, "Male", "Product Development", 2012, 35700.0));
		
		getMaleAndFemaleEmployee(employeeList);
		printAllDeparmentsName(employeeList);
		getAverageAgeOfMaleAndFemale(employeeList);
		getDetailsOfHighestPaidEmployee(employeeList);
		getNameOfAllEmpJoinAfter(employeeList);
		countNumberEmployeeInEachDept(employeeList);
		getAverageSalaryOfEachDepartment(employeeList);
		getYounestMaleEmpInDept(employeeList);
		getMostWorkingExperience(employeeList);
		getMaleAndFemaleEmployeeInSales(employeeList);
		getAverageSalaryOfMaleAndFemale(employeeList);
		getListOfNameInEachDept(employeeList);
		getAverageSalaryAndTotalSalaryOfWholeOrganization(employeeList);
		getOldestEmployee(employeeList);
		getListOfNameOfDuplicate(employeeList);
	}
	
	
	//Query 3.1 : How many male and female employees are there in the organization?
	public static void getMaleAndFemaleEmployee(List<Employee> list){
		Map<String,Long> map = list.stream().collect(Collectors.groupingBy(Employee::getGender,Collectors.counting()));
		System.out.println("Male and Female :"+map);
		//map = list.stream().collect(Collectors.groupingBy(Employee::getGender,Collectors.counting()));
	}
	//Query 3.2 : Print the name of all departments in the organization?
	public static void printAllDeparmentsName(List<Employee> list){
		list.stream().map(Employee::getDepartment)
					.distinct()
					.forEach(System.out::println);
	}
	//Query 3.3 : What is the average age of male and female employees?
	public static void getAverageAgeOfMaleAndFemale(List<Employee> list){
		Map<String,Double> map = list.stream().collect(Collectors.groupingBy(Employee::getGender,Collectors.averagingDouble(Employee::getAge)));
		System.out.println("Average Salary : "+map);
	}
	//Query 3.4 : Get the details of highest paid employee in the organization?
	public static void getDetailsOfHighestPaidEmployee(List<Employee> list){
		Optional<Employee> emp = list.stream().collect(Collectors.maxBy(Comparator.comparingDouble(Employee::getSalary)));
		System.out.println("Employee Highest Paid Employee :"+emp.get().getSalary());
	}
	//Query 3.5 : Get the names of all employees who have joined after 2015?
	public static void getNameOfAllEmpJoinAfter(List<Employee> list){
		list.stream().filter(e -> e.getYearOfJoining() > 2015)
					.map(Employee::getName)
					.forEach(System.out::println);
	}
	//Query 3.6 : Count the number of employees in each department?
	public static void countNumberEmployeeInEachDept(List<Employee> list){
		Map<String,Long> map = list.stream().collect(Collectors.groupingBy(Employee::getDepartment,Collectors.counting()));
		System.out.println("Count number of Employee in Each Department :"+map);
	}
	//Query 3.7 : What is the average salary of each department?
	public static void getAverageSalaryOfEachDepartment(List<Employee> list){
		Map<String,Double> map = list.stream().collect(Collectors.groupingBy(Employee::getDepartment,Collectors.averagingDouble(Employee::getSalary)));
		System.out.println("The Average Salary of Each department : "+map);
	}
	//Query 3.8 : Get the details of youngest male employee in the product development department?
	public static void getYounestMaleEmpInDept(List<Employee> list){
		Optional<Employee> emp = list.stream().filter(e->e.getGender()=="Male" && e.getDepartment() == "Product Development")
					.min(Comparator.comparing(Employee::getAge));
		System.out.println("Get the details of youngest :"+emp.get().getName());
	}
	//Query 3.9 : Who has the most working experience in the organization?
	public static void getMostWorkingExperience(List<Employee> list){
		Optional<Employee> emp = list.stream().sorted(Comparator.comparing(Employee::getYearOfJoining)).findFirst();
		System.out.println("Most working experience in the organization :"+emp.get().getName());
	}
	//Query 3.10 : How many male and female employees are there in the sales and marketing team?
	public static void getMaleAndFemaleEmployeeInSales(List<Employee> list){
		Map<String,Long> map = list.stream()
									.filter(e -> e.getDepartment()=="Sales And Marketing")
									.collect(Collectors.groupingBy(Employee::getGender,Collectors.counting()));
		System.out.println("Male and Female employee in the sales and marketing ::"+map);
	}
	//Query 3.11 : What is the average salary of male and female employees?
	public static void getAverageSalaryOfMaleAndFemale(List<Employee> list){
		Map<String,Double> map = list.stream()
									.collect(Collectors.groupingBy(Employee::getGender,Collectors.averagingDouble(Employee::getSalary)));
		System.out.println("Average salary of male and female employee::"+map);
	}
	//Query 3.12 : List down the names of all employees in each department?
	public static void getListOfNameInEachDept(List<Employee> list){
		Map<String,List<Employee>> map = list.stream()
										.collect(Collectors.groupingBy(Employee::getDepartment,Collectors.toList()));
		System.out.println("List down the names of all employee in each department");
		Set<Entry<String,List<Employee>>> entrySet = map.entrySet();
		for(Entry<String,List<Employee>> obj : entrySet){
			System.out.println("Department ---------- "+obj.getKey());
			for(Employee e : obj.getValue()){
				System.out.println(e.getName());
			}
		}
	}
	//Query 3.13 : What is the average salary and total salary of the whole organization?
	public static void getAverageSalaryAndTotalSalaryOfWholeOrganization(List<Employee> list){
		DoubleSummaryStatistics result = list.stream().collect(Collectors.summarizingDouble(Employee::getSalary));
		System.out.println("The average salary --"+result.getAverage());
		System.out.println("Total salary of whole organization -"+result.getSum());
		
	}
	//Query 3.14 : Separate the employees who are younger or equal to 25 years from those employees who are older than 25 years.
	public static void getSeparateEmpYounger(List<Employee> list){
		
	}
	//Query 3.15 : Who is the oldest employee in the organization? What is his age and which department he belongs to?
	public static void getOldestEmployee(List<Employee> list){
		Optional<Employee> emp = list.stream().max(Comparator.comparingInt(Employee::getAge));
		System.out.println("Oldest employee list :"+emp.get().getAge()+", department -"+emp.get().getDepartment());
		Stream<Employee> e = list.stream().sorted(Comparator.comparingInt(Employee::getAge));
		Iterator<Employee> i = e.iterator();
		while(i.hasNext()){
			System.out.println(i.next().getAge());
		}
	}
	
	//Query 3.16 : Given the list of employee, group them by employee name and print it.
	public static void getListOfNameOfDuplicate(List<Employee> list){
		Map<String,List<Employee>> map = list.stream()
										.collect(Collectors.groupingBy(Employee::getName,Collectors.toList()));
		System.out.println("List down the names of all employee ");
		Set<Entry<String,List<Employee>>> entrySet = map.entrySet();
		for(Entry<String,List<Employee>> obj : entrySet){
			System.out.println("Employee Name ---------- "+obj.getKey());
			for(Employee e : obj.getValue()){
				System.out.println(e.getName());
			}
		}
	}	
}

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *