From 5aab7f96a165d2c410ab35bba40a48f429333e1e Mon Sep 17 00:00:00 2001 From: Uma Vani Ravuri <umravuri@nisum.com> Date: Thu, 23 Sep 2021 06:04:57 +0000 Subject: [PATCH] Replace TestEmployee.java --- .../src/com/java8/practies/TestEmployee.java | 145 +++++++++++------- 1 file changed, 86 insertions(+), 59 deletions(-) diff --git a/Java8PractiesProgram/src/com/java8/practies/TestEmployee.java b/Java8PractiesProgram/src/com/java8/practies/TestEmployee.java index a0c9355..c381fa5 100644 --- a/Java8PractiesProgram/src/com/java8/practies/TestEmployee.java +++ b/Java8PractiesProgram/src/com/java8/practies/TestEmployee.java @@ -1,86 +1,113 @@ package com.java8.practies; import java.util.ArrayList; - +import java.util.Comparator; import java.util.List; import java.util.Map; +import java.util.Optional; +import java.util.function.Function; +import java.util.stream.Collector; import java.util.stream.Collectors; - +import java.util.stream.Stream; public class TestEmployee { public static void main(String[] args) { - - ArrayList<Employee> list = new ArrayList<Employee>(); - list.add(new Employee("1", "Uma", "Vani", "IT")); - list.add(new Employee("2", "Sujit", "Krishnan", "CSE")); - list.add(new Employee("3", "Brayan", "Bra", "IT")); - list.add(new Employee("4", "Allice", "Ali", "CSE")); - - list.add(new Employee("5", "Sco te", "Scote", "IT")); - list.add(new Employee("6", "Rajesh", "Krishnan", "ECE")); - list.add(new Employee("7", "Ramana", "Raju", "IT")); - list.add(new Employee("8", "Raju", "Krishnan", "EEE")); - - list.add(new Employee("9", "Rishi", "Vani", "IT")); - list.add(new Employee("10", "Dattu", "Krishnan", "EEE")); - - //1)list of employees, get the first employee from the list and return his fullName - list.stream().limit(1) - .forEach(x -> System.out.println("First employee Full Name is::" + x.firstName + x.lastName)); - - //given a list of employees. Can you create a map with the count of employees each department has ? - //with key as department name and count of employees as value. - Map<String, Long> mapDepartment = list.stream() - .collect(Collectors.groupingBy(Employee::getDepartmentNames, Collectors.counting())); - mapDepartment.forEach((k, v) -> System.out.println((k + ":" + v))); - - //Given a map with the department name as key and value as list of employees belonging to that department. - //when a search string is given, need to list out the employees whose firstname or lastname is matching(match should be case insensitive) - Map<String, List<Employee>> mapEmployee = list.stream() - .collect(Collectors.groupingBy(Employee::getDepartmentNames, Collectors.toList())); - mapEmployee.forEach((k, v) -> System.out.println((k + ":" + v))); - - //Consider a list of employees, and if a department name is given as argument, list out the employees which doesn't belong to that department. - - List<Employee> filterEmp = list.stream().filter(x -> !x.getDepartmentNames().equals("IT")) - .collect(Collectors.toList()); - filterEmp.forEach(x -> System.out.println(" Employee list whose Department is not IT" + x)); + List<String> depList = new ArrayList<String>(); + depList.add("IT"); + depList.add("CSE"); + depList.add("ECE"); + + List<String> depList2 = new ArrayList<String>(); + depList2.add("ECE"); + depList2.add("EEE"); + depList2.add("IT"); + + List<Employee> listEmp = new ArrayList<Employee>(); + listEmp.add(new Employee("1", "Uma", "vani", depList)); + listEmp.add(new Employee("2", "Sujit", "Krishnan", depList2)); + listEmp.add(new Employee("3", "Brayan", "Braya", depList)); + listEmp.add(new Employee("4", "Allice", "Ali", depList2)); + listEmp.add(new Employee("5", "Sco te","", depList2)); + listEmp.add(new Employee("6", "Rajesh", "Krishnan", depList)); + listEmp.add(new Employee("7", "Ramana", "Raju", depList2)); + listEmp.add(new Employee("8", "Raju", "Krishnan", depList)); + listEmp.add(new Employee("9", "Rishi", "Vani", depList)); + listEmp.add(new Employee("10", "Dattu", "", depList2)); + + + //list of employees, get the first employee from the list and return his fullName. - // 6)Consider a list of employees, sort the employees by their firstName and - // return the sorted list of employees. - List<Employee> sortedEmployee = list.stream().sorted(new EmployeeComparator()).collect(Collectors.toList()); - sortedEmployee.forEach(x -> System.out.println("Sorted Employee based on firstName" + x)); + listEmp.stream().map(emp -> { + Optional.ofNullable(emp) + .ifPresent(x -> System.out.println("First Employee Details :" + x.getFirstName() + x.getLastName())); + return emp; + }).findFirst(); + + + //Consider a list of employees, sort the employees by their firstName and + listEmp.stream().sorted(Comparator.comparing(Employee::getFirstName)).collect(Collectors.toList()).forEach(System.out::println); + //Consider a list of employees, return the employee whose empId is highest - Employee e = list.stream().max((e1, e2) -> e1.getId().compareTo(e2.getId())).get(); - System.out.println("Maximum Id Employee details" + e); - + Optional<Employee> e = listEmp.stream().collect(Collectors.maxBy(Comparator.comparing(Employee::getId))); + if (e.isPresent()) { + System.out.print("Higest Employee Id"+e.get()); + } + //Consider a list of employees, concat the fullName of all the employees with pipe (|) and return the concatenated string. - String joinedString = list.stream().map(x -> x.firstName + x.lastName).collect(Collectors.joining("|")); - System.out.println("Maximum Id Employee details-----------" + joinedString); + + String joinedString = listEmp.stream().map(x -> x.firstName + x.lastName).collect(Collectors.joining("|")); + System.out.println("FullNames Of all employees concat with | symbol is :-----------" + joinedString); + //Consider a list of 10 employees, get the 8th employee and print his full name and department name. - list.stream().sorted((e1, e2) -> e1.getId().compareTo(e2.getId())).skip(8).limit(1) - .forEach(System.out::println); + listEmp.stream().sorted(Comparator.comparing(Employee::getId)).skip(8).limit(1).forEach(System.out::println); //consider list of employees and list of employee ids. return the list of employees matching with the given list of employee ids. - List<Employee> checkForMatches = new ArrayList<>(); - checkForMatches.add(new Employee("2", "siri", "raja", "CSE")); - checkForMatches.add(new Employee("7", "roja", "rani", "CSE")); - checkForMatches.add(new Employee("11", "roja", "rani", "CSE")); - + List<Employee> checkForMatches = new ArrayList<>(); + checkForMatches.add(new Employee("2", "siri", "raja", depList)); + checkForMatches.add(new Employee("7", "roja", "rani", depList2)); + checkForMatches.add(new Employee("11", "roja", "rani", depList)); + - List<Employee> filteredList = list.stream() - .filter(employee -> checkForMatches.stream().anyMatch(dept -> employee.getId().equals(dept.getId()))) - .collect(Collectors.toList()); + listEmp.stream().filter(employee -> checkForMatches.stream().anyMatch(dept -> employee.getId().equals(dept.getId()))) + .collect(Collectors.toList()).forEach(System.out::println); - filteredList.forEach(x -> System.out.println("Employee matches" + x)); //Store ids are of 4 digit strings.if length of given storeId is less than 4 digits,you need to prefix with zeros and return a 4 digit storeId. - list.stream().forEach(x->System.out.println("4 Digits employee Id::"+String.format("%04d", Integer.parseInt(x.id)))); + + listEmp.stream() + .map(employee -> { + Optional.ofNullable(employee) + .map(Employee::getId) + .map(id -> String.format("%04d", Integer.parseInt(id))) + .ifPresent(a -> employee.setId(a)); + return employee.getId(); + }).collect(Collectors.toList()).forEach(System.out::println); + + + //Consider a list of employees, and if a department name is given as argument, list out the employees which doesn't belong to that department. + listEmp.stream().filter(x -> !x.getDepartmentNames().stream().equals("EEE")) + .collect(Collectors.toList()).forEach(System.out::println); + + listEmp.stream() + .flatMap(e -> e.getDepartmentNames().stream()) + .filter(d -> !"EEE".equals(d.getDepartmentNames())) + .collect(Collectors.toList()).forEach(System.out::println); + + + //given a list of employees. Can you create a map with the count of employees each department has ? + //with key as department name and count of employees as value. + Stream<String> distinctDept=listEmp.stream() + .map(Employee::getDepartmentNames) + .flatMap(x->x.stream()); + Map<String, Long> mapDepartment = distinctDept.collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); + mapDepartment.forEach((k, v) -> System.out.println((k + ":" + v))); + + } } -- 2.18.1