Task - Collection - Employee Statistics #57
Replies: 39 comments
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
package collections; class Employee1 public Employee1(int id, String name, int age, String gender, String department, int yearOfJoining, double salary) public int getId() public String getName() public int getAge() public String getGender() public String getDepartment() public int getYearOfJoining() public double getSalary() @OverRide public class EmployeeStatitics // to print number of employees in each department // to print average salary Map<String, Double> averageSalary = employeelist.stream() // most salary paid to // average age Map<String, Double> age = employeelist.stream().collect(Collectors.groupingBy(Employee1::getDepartment, Collectors.averagingDouble(Employee1::getAge))); //Senior employee in organization Employee1 seniorMost = employeelist.stream() // the youngest employee in the organization //to get male and female count Map<String, Long> genderCount = employeelist.stream().collect(Collectors.groupingBy(Employee1::getGender, Collectors.counting())); } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
class Employee57 {
int id;
String name;
int age;
String gender;
String department;
int yearOfJoining;
double salary;
public Employee57(int id, String name, int age, String gender, String department, int yearOfJoining,
double salary) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.department = department;
this.yearOfJoining = yearOfJoining;
this.salary = salary;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public String getDepartment() {
return department;
}
public int getYearOfJoining() {
return yearOfJoining;
}
public double getSalary() {
return salary;
}
@Override
public String toString() {
return "Id : " + id
+ ", Name : " + name
+ ", age : " + age
+ ", Gender : " + gender
+ ", Department : " + department
+ ", Year Of Joining : " + yearOfJoining
+ ", Salary : " + salary;
}
}
public class discussion57 {
public static void main(String[] args) {
Employee57 emp1 = new Employee57(3, "Vishal Singhal", 34, "male", "HR", 2013, 300000);
Employee57 emp2 = new Employee57(4, "Amitabh Singh", 35, "male", "Admin", 2014, 500000);
Employee57 emp3 = new Employee57(5, "Vivek Bhati", 28, "male", "Admin", 2017, 500000);
Employee57 emp4 = new Employee57(6, "Vipul Diwan", 34, "male", "Account", 2014, 200000);
Employee57 emp5 = new Employee57(7, "Satish Kumar", 29, "male", "Account", 2017, 75000);
Employee57 emp6 = new Employee57(8, "Geetika Chauhan", 30, "female", "Admin", 2016, 90000);
ArrayList<Employee57> list = new ArrayList<>();
list.add(emp1);
list.add(emp2);
list.add(emp3);
list.add(emp4);
list.add(emp5);
list.add(emp6);
// TASK 1
Map<String, Long> count = list.stream()
.collect(Collectors.groupingBy(Employee57::getDepartment, Collectors.counting()));
System.out.println(count);
// TASK 2
Map<String, Double> averageSalary = list.stream()
.collect(Collectors.groupingBy(Employee57::getGender,
Collectors.averagingDouble(Employee57::getSalary)));
System.out.println(averageSalary);
// task3
Optional<Employee57> highestPaid = list.stream().max(Comparator.comparingDouble(Employee57::getSalary));
System.out.println(highestPaid);
// task 4
Map<String, Double> averageAge = list.stream()
.collect(
Collectors.groupingBy(Employee57::getDepartment,
Collectors.averagingDouble(Employee57::getAge)));
System.out.println(averageAge);
// task 5
Optional<Employee57> mostExperienced = list.stream()
.min(Comparator.comparingInt(Employee57::getYearOfJoining));
System.out.println(mostExperienced);
// task 6
Optional<Employee57> youngest = list.stream()
.min(Comparator.comparingInt(Employee57::getAge));
System.out.println(youngest);
// task 7
Map<String, Long> countt = list.stream()
.collect(Collectors.groupingBy(Employee57::getDepartment, Collectors.counting()));
System.out.println("No. of employees in each department :" + countt);
// task 8
Map<String, Long> genderCount = list.stream()
.collect(Collectors.groupingBy(Employee57::getGender, Collectors.counting()));
System.out.println("No. of male and female employees in each department : " + genderCount);
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
class Employee {
int id;
String name;
int age;
String gender;
String department;
int yearOfJoining;
double salary;
public Employee(int id, String name, int age, String gender, String department, int yearOfJoining, double salary) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.department = department;
this.yearOfJoining = yearOfJoining;
this.salary = salary;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public String getDepartment() {
return department;
}
public int getYearOfJoining() {
return yearOfJoining;
}
public double getSalary() {
return salary;
}
@Override
public String toString() {
return "Id : " + id
+ ", Name : " + name
+ ", age : " + age
+ ", Gender : " + gender
+ ", Department : " + department
+ ", Year Of Joining : " + yearOfJoining
+ ", Salary : " + salary;
}
}
public class d57 {
public static void main(String[] args) {
Employee emp1 = new Employee(3, "Vishal Singhal", 34, "male", "HR", 2013, 300000);
Employee emp2 = new Employee(4, "Amitabh Singh", 35, "male", "Admin", 2014, 500000);
Employee emp3 = new Employee(5, "Vivek Bhati", 28, "male", "Admin", 2017, 500000);
Employee emp4 = new Employee(6, "Vipul Diwan", 34, "male", "Account", 2014, 200000);
Employee emp5 = new Employee(7, "Satish Kumar", 29, "male", "Account", 2017, 75000);
Employee emp6 = new Employee(8, "Geetika Chauhan", 30, "female", "Admin", 2016, 90000);
List<Employee> employees = Arrays.asList(emp1, emp2, emp3, emp4, emp5, emp6);
System.out.println(employees.stream().collect(Collectors.groupingBy(Employee::getDepartment, Collectors.counting())));
System.out.println(employees.stream().collect(Collectors.groupingBy(Employee::getGender, Collectors.averagingDouble(Employee::getSalary))));
System.out.println(employees.stream().max((e1, e2) -> (int) (e1.getSalary() - e2.getSalary())));
System.out.println(employees.stream().collect(Collectors.groupingBy(Employee::getDepartment, Collectors.averagingDouble(Employee::getAge))));
System.out.println(employees.stream().max((e1, e2) -> (e1.getYearOfJoining() - e2.getYearOfJoining())));
System.out.println(employees.stream().min((e1, e2) -> (e1.getAge() - e2.getAge())));
System.out.println(employees.stream().collect(Collectors.groupingBy(Employee::getGender, Collectors.counting())));
}
} |
Beta Was this translation helpful? Give feedback.
-
package java_programs;
import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
class EmployeeData1 {
int id;
String name;
int age;
String gender;
String department;
int yearOfJoining;
double salary;
public EmployeeData1(int id, String name, int age, String gender, String department, int yearOfJoining,
double salary) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.department = department;
this.yearOfJoining = yearOfJoining;
this.salary = salary;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public String getDepartment() {
return department;
}
public int getYearOfJoining() {
return yearOfJoining;
}
public double getSalary() {
return salary;
}
@Override
public String toString() {
return "Id : " + id
+ ", Name : " + name
+ ", age : " + age
+ ", Gender : " + gender
+ ", Department : " + department
+ ", Year Of Joining : " + yearOfJoining
+ ", Salary : " + salary;
}
}
public class EmployeeStatics {
public static void main(String[] args) {
EmployeeData1 emp1 = new EmployeeData1(3, "Vishal Singhal", 34, "male", "HR", 2013, 300000);
EmployeeData1 emp2 = new EmployeeData1(4, "Amitabh Singh", 35, "male", "Admin", 2014, 500000);
EmployeeData1 emp3 = new EmployeeData1(5, "Vivek Bhati", 28, "male", "Admin", 2017, 500000);
EmployeeData1 emp4 = new EmployeeData1(6, "Vipul Diwan", 34, "male", "Account", 2014, 200000);
EmployeeData1 emp5 = new EmployeeData1(7, "Satish Kumar", 29, "male", "Account", 2017, 75000);
EmployeeData1 emp6 = new EmployeeData1(8, "Geetika Chauhan", 30, "female", "Admin", 2016, 90000);
List<EmployeeData1> employees = new ArrayList<EmployeeData1>();
employees.add(emp1);
employees.add(emp2);
employees.add(emp3);
employees.add(emp4);
employees.add(emp5);
employees.add(emp6);
Map<String, Long> count = employees.stream()
.collect(Collectors.groupingBy(EmployeeData1::getDepartment, Collectors.counting()));
System.out.println(count);
// employees.stream().collect(Collectors.groupingBy(e -> e.getGender(),
// Collectors.averagingDouble(e -> e.getSalary())));////why this is not working as lamda expression ended so again we have to start lamda if want to use otherwise we can use function referencing
// as lamda expression ends there only again we required a
Map<String, Double> data = employees.stream().collect(
Collectors.groupingBy(e -> e.getGender(), Collectors.averagingDouble(EmployeeData1::getSalary)));
System.out.println(data);
// Get the details of the highest-paid employee in the organization from the
// given list of employees.
Comparator<EmployeeData1> comparedBySalary = (obj1, obj2) -> (int) obj1.getSalary() - (int) obj2.getSalary();
Collections.sort(employees, comparedBySalary);
System.out.println(employees.get(0));
employees.stream().sorted(comparedBySalary).limit(1).forEach(emp -> System.out.println(emp));
// Get the average age of each department in an organization.
Map<String, Double> averageAge = employees.stream()
.collect(Collectors.groupingBy(e -> e.getDepartment(), Collectors.averagingInt(EmployeeData1::getAge)));
System.out.println(averageAge);
// Find out who is the senior-most employee in the organization in terms of
// experience.
Optional<EmployeeData1> mostExperiencedEmployee = employees.stream()
.min(Comparator.comparingInt(EmployeeData1::getAge));
System.out.println(mostExperiencedEmployee);
// Get the details of the youngest employee in the organization.
Optional<EmployeeData1> minorExperiencedEmployee = employees.stream()
.max(Comparator.comparingInt(EmployeeData1::getAge));
System.out.println(minorExperiencedEmployee);
// Get the number of employees in each department if you have given a list of
// employees.
HashMap<String, Integer> personByDepartment = new HashMap<>();
for (EmployeeData1 e : employees) {
if (personByDepartment.containsKey(e.getDepartment()))
personByDepartment.put(e.getDepartment(), personByDepartment.get(e.getDepartment()) + 1);
else
personByDepartment.put(e.getDepartment(), 1);
}
// int value1 = personByDepartment.get("Admin");
// for (Map.Entry entry : personByDepartment.entrySet()) {
// System.out.println(entry.getKey() + " " + entry.getValue());
// }
personByDepartment.forEach((key, value) -> System.out.println(key + " " + value));
// Find out the number of male and female employees in the organization.
Map<String, Long> genderCount = employees.stream()
.collect(Collectors.groupingBy(e -> e.getGender(), Collectors.counting()));
System.out.println(genderCount);
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.*;
import java.util.stream.Collectors;
public class task57 {
public static void main(String[] args) {
Employeee emp1 = new Employeee(3, "Vishal Singhal", 34, "male", "HR", 2013, 300000);
Employeee emp2 = new Employeee(4, "Amitabh Singh", 35, "male", "Admin", 2014, 500000);
Employeee emp3 = new Employeee(5, "Vivek Bhati", 28, "male", "Admin", 2017, 500000);
Employeee emp4 = new Employeee(6, "Vipul Diwan", 34, "male", "Account", 2014, 200000);
Employeee emp5 = new Employeee(7, "Satish Kumar", 29, "male", "Account", 2017, 75000);
Employeee emp6 = new Employeee(8, "Geetika Chauhan", 30, "female", "Admin", 2016, 90000);
ArrayList<Employeee> emplist = new ArrayList<Employeee>();
emplist.add(emp1);
emplist.add(emp2);
emplist.add(emp3);
emplist.add(emp4);
emplist.add(emp5);
emplist.add(emp6);
System.out.println("Count the number of employees in each department");
System.out
.println(emplist.stream().collect(Collectors.groupingBy(Employeee::getDepartment, Collectors.counting())));
System.out.println();
System.out.println("Find out the average salary of male and female employees.");
System.out.println(emplist.stream()
.collect(Collectors.groupingBy(Employeee::getGender, Collectors.averagingDouble(Employeee::getSalary))));
System.out.println();
System.out
.println("Get the details of the highest-paid employee in the organization from the given list of employees.");
System.out.println(emplist.stream().max(Comparator.comparingDouble(Employeee::getSalary)));
System.out.println();
System.out.println("Get the average age of each department in an organization.");
System.out.println(emplist.stream()
.collect(Collectors.groupingBy(Employeee::getDepartment, Collectors.averagingDouble(Employeee::getAge))));
System.out.println();
System.out.println("Find out who is the senior-most employee in the organization in terms of experience.");
System.out.println(emplist.stream().max(Comparator.comparing(Employeee::getAge)));
System.out.println();
System.out.println("Get the details of the youngest employee in the organization.");
System.out.println(emplist.stream().min(Comparator.comparing(Employeee::getAge)));
System.out.println();
System.out.println("Find out the number of male and female employees in the organization.");
System.out.println(emplist.stream().collect(Collectors.groupingBy(Employeee::getGender, Collectors.counting())));
}
}
class Employeee {
int id;
String name;
int age;
String gender;
String department;
int yearOfJoining;
double salary;
public Employeee(int id, String name, int age, String gender, String department, int yearOfJoining, double salary) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.department = department;
this.yearOfJoining = yearOfJoining;
this.salary = salary;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public String getDepartment() {
return department;
}
public int getYearOfJoining() {
return yearOfJoining;
}
public double getSalary() {
return salary;
}
@Override
public String toString() {
return "Id : " + id
+ ", Name : " + name
+ ", age : " + age
+ ", Gender : " + gender
+ ", Department : " + department
+ ", Year Of Joining : " + yearOfJoining
+ ", Salary : " + salary;
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
public class D57 {
public static void main(String[] args) {
Employee1 emp1 = new Employee1(3, "Vishal Singhal", 34, "male", "HR", 2013, 300000);
Employee1 emp2 = new Employee1(4, "Amitabh Singh", 35, "male", "Admin", 2014, 500000);
Employee1 emp3 = new Employee1(5, "Vivek Bhati", 28, "male", "Admin", 2017, 500000);
Employee1 emp4 = new Employee1(6, "Vipul Diwan", 34, "male", "Account", 2014, 200000);
Employee1 emp5 = new Employee1(7, "Satish Kumar", 29, "male", "Account", 2017, 75000);
Employee1 emp6 = new Employee1(8, "Geetika Chauhan", 30, "female", "Admin", 2016, 90000);
ArrayList<Employee1> employeeList = new ArrayList<>();
employeeList.add(emp1);
employeeList.add(emp2);
employeeList.add(emp3);
employeeList.add(emp4);
employeeList.add(emp5);
employeeList.add(emp6);
// task1
Map<String, Long> count = employeeList.stream()
.collect(Collectors.groupingBy(Employee1::getDepartment, Collectors.counting()));
System.out.println("No of employees in each department " + count);
// task2
Map<String, Double> averageSalary = employeeList.stream()
.collect(Collectors.groupingBy(Employee1::getGender, Collectors.averagingDouble(Employee1::getSalary)));
System.out.println("Average salary of male and female employees : " + averageSalary);
// task3
Optional<Employee1> highestPaid = employeeList.stream().max(Comparator.comparingDouble(Employee1::getSalary));
System.out.println("Details of highest paid employee : " + highestPaid);
// task 4
Map<String, Double> averageAge = employeeList.stream()
.collect(
Collectors.groupingBy(Employee1::getDepartment, Collectors.averagingDouble(Employee1::getAge)));
System.out.println("Average age in each department : " + averageAge);
// task 5
Optional<Employee1> mostExperienced = employeeList.stream()
.min(Comparator.comparingInt(Employee1::getYearOfJoining));
System.out.println("Senior most employee in terms of experience : " + mostExperienced);
// task 6
Optional<Employee1> youngest = employeeList.stream()
.min(Comparator.comparingInt(Employee1::getAge));
System.out.println("Youngest employee in the organisation : " + youngest);
// task 7
Map<String, Long> countt = employeeList.stream()
.collect(Collectors.groupingBy(Employee1::getDepartment, Collectors.counting()));
System.out.println("No. of employees in each department :" + countt);
// task 8
Map<String, Long> genderCount = employeeList.stream()
.collect(Collectors.groupingBy(Employee1::getGender, Collectors.counting()));
System.out.println("No. of male and female employees in each department : " + genderCount);
}
}
class Employee1 {
int id;
String name;
int age;
String gender;
String department;
int yearOfJoining;
double salary;
public Employee1(int id, String name, int age, String gender, String department, int yearOfJoining, double salary) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.department = department;
this.yearOfJoining = yearOfJoining;
this.salary = salary;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public String getDepartment() {
return department;
}
public int getYearOfJoining() {
return yearOfJoining;
}
public double getSalary() {
return salary;
}
@Override
public String toString() {
return "Id : " + id
+ ", Name : " + name
+ ", age : " + age
+ ", Gender : " + gender
+ ", Department : " + department
+ ", Year Of Joining : " + yearOfJoining
+ ", Salary : " + salary;
}
} |
Beta Was this translation helpful? Give feedback.
-
package com.lingu;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collector;
import java.util.stream.Collectors;
public class Employee {
int id;
String name;
int age;
String gender;
String department;
int yearOfJoining;
double salary;
public Employee(int id, String name, int age, String gender, String department, int yearOfJoining, double salary)
{
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.department = department;
this.yearOfJoining = yearOfJoining;
this.salary = salary;
}
public int getId()
{
return id;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String getGender()
{
return gender;
}
public String getDepartment()
{
return department;
}
public int getYearOfJoining()
{
return yearOfJoining;
}
public double getSalary()
{
return salary;
}
@Override
public String toString()
{
return "Id : "+id
+", Name : "+name
+", age : "+age
+", Gender : "+gender
+", Department : "+department
+", Year Of Joining : "+yearOfJoining
+", Salary : "+salary;
}
public static void main(String[] args) {
Employee emp1 = new Employee(3, "Vishal Singhal", 34, "male", "HR", 2013, 300000);
Employee emp2 = new Employee(4, "Amitabh Singh", 35, "male", "Admin", 2014, 500000);
Employee emp3 = new Employee(5, "Vivek Bhati", 28, "male", "Admin", 2017, 500000);
Employee emp4 = new Employee(6, "Vipul Diwan", 34, "male", "Account", 2014, 200000);
Employee emp5 = new Employee(7, "Satish Kumar", 29, "male", "Account", 2017, 75000);
Employee emp6 = new Employee(8, "Geetika Chauhan", 30, "female", "Admin", 2016, 90000);
ArrayList<Employee> employeeList= new ArrayList();
employeeList.add(emp1);
employeeList.add(emp2);
employeeList.add(emp3);
employeeList.add(emp4);
employeeList.add(emp5);
employeeList.add(emp6);
Map<String, Long> employeeCountByDepartment = employeeList.stream().collect(Collectors.groupingBy(Employee::getDepartment,Collectors.counting()));
System.out.println(employeeCountByDepartment);
Map<String, Double> avgSalaryOfMaleAndFemaleEmployees = employeeList.stream().collect(Collectors.groupingBy(Employee::getGender,Collectors.averagingDouble(Employee::getSalary)));
System.out.println(avgSalaryOfMaleAndFemaleEmployees);
Optional<Employee> detailsofhighestpaidemployee = employeeList.stream().collect(Collectors.maxBy(Comparator.comparingDouble(Employee::getSalary)));
System.out.println(detailsofhighestpaidemployee);
Map<String, Double> averageageofeachdepartment = employeeList.stream().collect(Collectors.groupingBy(Employee::getDepartment,Collectors.averagingDouble(Employee::getAge)));
System.out.println(averageageofeachdepartment);
Optional<Employee> seniorMostEmployeeWrapper = employeeList.stream().sorted(Comparator.comparingInt(Employee::getYearOfJoining)).findFirst();
System.out.println(seniorMostEmployeeWrapper);
Optional<Employee> youngestemployeeintheorganization = employeeList.stream().min(Comparator.comparingInt(Employee::getAge));
System.out.println(youngestemployeeintheorganization);
Map<String, Long> numberofemployeesineachdepartment = employeeList.stream().collect(Collectors.groupingBy(Employee::getDepartment,Collectors.counting()));
System.out.println(numberofemployeesineachdepartment);
Map<String, Long> numberofmaleandfemaleemployees = employeeList.stream().collect(Collectors.groupingBy(Employee::getGender,Collectors.counting()));
System.out.println(numberofmaleandfemaleemployees);
}
}
|
Beta Was this translation helpful? Give feedback.
-
import java.util.*;
import java.util.stream.Collectors;
public class Driver {
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee emp1 = new Employee(3, "Vishal Singhal", 34, "male", "HR", 2013, 300000);
Employee emp2 = new Employee(4, "Amitabh Singh", 35, "male", "Admin", 2014, 500000);
Employee emp3 = new Employee(5, "Vivek Bhati", 28, "male", "Admin", 2017, 500000);
Employee emp4 = new Employee(6, "Vipul Diwan", 34, "male", "Account", 2014, 200000);
Employee emp5 = new Employee(7, "Satish Kumar", 29, "male", "Account", 2017, 75000);
Employee emp6 = new Employee(8, "Geetika Chauhan", 30, "female", "Admin", 2016, 90000);
List<Employee> employees = new ArrayList<>();
employees.add(emp1);
employees.add(emp2);
employees.add(emp3);
employees.add(emp4);
employees.add(emp5);
employees.add(emp6);
//Calculating no. of emp in department
List<Employee> x = employees.stream().filter( o -> o.department.equals("HR")).toList();
List<Employee> y = employees.stream().filter( o -> o.department.equals("Account")).toList();
List<Employee> z = employees.stream().filter( o -> o.department.equals("Admin")).toList();
OptionalDouble avgMen = employees.stream().filter(o -> o.gender.equalsIgnoreCase("male")).mapToDouble(Employee::getSalary).average();
OptionalDouble avgFemale = employees.stream().filter(o -> o.gender.equalsIgnoreCase("female")).mapToDouble(Employee::getSalary).average();
System.out.println("{female=" + avgFemale.getAsDouble() +", male=" + avgMen.getAsDouble() + "}");
OptionalDouble highest = employees.stream().mapToDouble(Employee::getSalary).max();
OptionalDouble mostExp = employees.stream().mapToDouble(Employee::getYearOfJoining).min();
OptionalDouble minExp = employees.stream().mapToDouble(Employee::getYearOfJoining).max();
for(Employee e : employees) {
if(e.getSalary() == highest.getAsDouble()) {
System.out.println(e);
break;
}
}
OptionalDouble avgAccount = employees.stream().filter(o -> o.department.equalsIgnoreCase("Account")).mapToDouble(Employee::getAge).average();
OptionalDouble avgAdmin = employees.stream().filter(o -> o.department.equalsIgnoreCase("Admin")).mapToDouble(Employee::getAge).average();
OptionalDouble avgAHr = employees.stream().filter(o -> o.department.equalsIgnoreCase("HR")).mapToDouble(Employee::getAge).average();
System.out.println("{ Account=" +avgAccount.getAsDouble() + ", HR=" + avgAHr.getAsDouble() + ", Admin=" + avgAdmin.getAsDouble() + "}");
for(Employee e : employees) {
if(e.getYearOfJoining() == minExp.getAsDouble()) {
System.out.println(e);
break;
}
}
for(Employee e : employees) {
if(e.getYearOfJoining() == mostExp.getAsDouble()) {
System.out.println(e);
break;
}
}
System.out.println("{Account=" + y.size() +", HR=" + x.size() + ", Admin=" + z.size() + "}");
Map<String, Long> genderCount = employees.stream().collect(Collectors.groupingBy(Employee::getGender, Collectors.counting()));
System.out.println( genderCount);
}
} |
Beta Was this translation helpful? Give feedback.
-
Codeimport java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
Employee emp1 = new Employee(3, "Vishal Singhal", 34, "male", "HR", 2013, 300000);
Employee emp2 = new Employee(4, "Amitabh Singh", 35, "male", "Admin", 2014, 500000);
Employee emp3 = new Employee(5, "Vivek Bhati", 28, "male", "Admin", 2017, 500000);
Employee emp4 = new Employee(6, "Vipul Diwan", 34, "male", "Account", 2014, 200000);
Employee emp5 = new Employee(7, "Satish Kumar", 29, "male", "Account", 2017, 75000);
Employee emp6 = new Employee(8, "Geetika Chauhan", 30, "female", "Admin", 2016, 90000);
List<Employee> employee = new ArrayList<Employee>();
employee.add(emp1);
employee.add(emp2);
employee.add(emp3);
employee.add(emp4);
employee.add(emp5);
employee.add(emp6);
Map<String, Long> collect = employee.stream().collect(Collectors.groupingBy(e -> e.getDepartment(),Collectors.counting()));
System.out.println(collect);
employee.stream().count();
Map<String, Double> collect2 = employee.stream().collect(Collectors.groupingBy(Employee::getGender,Collectors.averagingDouble(Employee::getSalary)));
System.out.println(collect2);
Optional<Employee> findFirst = employee.stream().sorted(Comparator.comparingDouble(Employee::getSalary).reversed()).findFirst();
System.out.println(findFirst);
Map<String, Double> collect3 = employee.stream().collect(Collectors.groupingBy(Employee::getDepartment,Collectors.averagingInt(Employee::getAge)));
System.out.println(collect3);
Optional<Employee> findFirst2 = employee.stream().sorted(Comparator.comparingInt(Employee::getYearOfJoining)).findFirst();
System.out.println(findFirst2);
Optional<Employee> findFirst3 = employee.stream().sorted(Comparator.comparingInt(Employee::getAge)).findFirst();
System.out.println(findFirst3);
Map<String, Long> collect4 = employee.stream().collect(Collectors.groupingBy(Employee::getGender,Collectors.counting()));
System.out.println(collect4);
}
}
class Employee
{
int id;
String name;
int age;
String gender;
String department;
int yearOfJoining;
double salary;
public Employee(int id, String name, int age, String gender, String department, int yearOfJoining, double salary)
{
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.department = department;
this.yearOfJoining = yearOfJoining;
this.salary = salary;
}
public int getId()
{
return id;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String getGender()
{
return gender;
}
public String getDepartment()
{
return department;
}
public int getYearOfJoining()
{
return yearOfJoining;
}
public double getSalary()
{
return salary;
}
@Override
public String toString()
{
return "Id : "+id
+", Name : "+name
+", age : "+age
+", Gender : "+gender
+", Department : "+department
+", Year Of Joining : "+yearOfJoining
+", Salary : "+salary;
}
} |
Beta Was this translation helpful? Give feedback.
-
package p1;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.stream.Collectors;
class Employee{
int id;
String name;
int age;
String gender;
String department;
int yearOfJoining;
double salary;
public Employee(int id, String name, int age, String gender, String department, int yearOfJoining, double salary) {
super();
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.department = department;
this.yearOfJoining = yearOfJoining;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public int getYearOfJoining() {
return yearOfJoining;
}
public void setYearOfJoining(int yearOfJoining) {
this.yearOfJoining = yearOfJoining;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", gender=" + gender + ", department="
+ department + ", yearOfJoining=" + yearOfJoining + ", salary=" + salary + "]";
}
}
public class problem57 {
public static void main(String[] args) {
Employee emp1 = new Employee(3, "Vishal Singhal", 34, "male", "HR", 2013, 300000);
Employee emp2 = new Employee(4, "Amitabh Singh", 35, "male", "Admin", 2014, 500000);
Employee emp3 = new Employee(5, "Vivek Bhati", 28, "male", "Admin", 2017, 500000);
Employee emp4 = new Employee(6, "Vipul Diwan", 34, "male", "Account", 2014, 200000);
Employee emp5 = new Employee(7, "Satish Kumar", 29, "male", "Account", 2017, 75000);
Employee emp6 = new Employee(8, "Geetika Chauhan", 30, "female", "Admin", 2016, 90000);
ArrayList<Employee> list = new ArrayList<>();
list.add(emp1);
list.add(emp2);
list.add(emp3);
list.add(emp4);
list.add(emp5);
list.add(emp6);
Map<String, Long> collect = list.stream().collect(Collectors.groupingBy(Employee::getDepartment,Collectors.counting()));
System.out.println(collect);
System.out.println();
Map<String, Double> collect2 = list.stream().collect(Collectors.groupingBy(Employee::getGender,Collectors.averagingDouble(Employee::getSalary)));
System.out.println(collect2);
System.out.println();
Double double1 = list.stream().map(x->x.getSalary()).sorted(Comparator.reverseOrder()).findFirst().get();
System.out.println(double1);
System.out.println();
Map<String, Double> collect3 = list.stream().collect(Collectors.groupingBy(Employee::getDepartment,Collectors.averagingDouble(Employee::getAge)));
System.out.println(collect3);
System.out.println();
Optional<Employee> findFirst = list.stream().sorted(Comparator.comparingInt(Employee::getYearOfJoining)).findFirst();
System.out.println(findFirst);
System.out.println();
Optional<Employee> findFirst2 = list.stream().sorted(Comparator.comparing(Employee::getAge)).findFirst();
System.out.println(findFirst2);
System.out.println();
Map<String, Long> collect4 = list.stream().collect(Collectors.groupingBy(Employee::getGender,Collectors.counting()));
System.out.println(collect4);
}
} |
Beta Was this translation helpful? Give feedback.
-
codepackage assignment57;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
public class EmployeeList {
public static void main(String[] args) {
List<Employee> employeeList = new ArrayList<>();
Employee emp1 = new Employee(3, "Vishal Singhal", 34, "male", "HR", 2013, 300000);
Employee emp2 = new Employee(4, "Amitabh Singh", 35, "male", "Admin", 2014, 500000);
Employee emp3 = new Employee(5, "Vivek Bhati", 28, "male", "Admin", 2017, 500000);
Employee emp4 = new Employee(6, "Vipul Diwan", 34, "male", "Account", 2014, 200000);
Employee emp5 = new Employee(7, "Satish Kumar", 29, "male", "Account", 2017, 75000);
Employee emp6 = new Employee(8, "Geetika Chauhan", 30, "female", "Admin", 2016, 90000);
employeeList.add(emp1);
employeeList.add(emp2);
employeeList.add(emp3);
employeeList.add(emp4);
employeeList.add(emp5);
employeeList.add(emp6);
List<Employee> list = Arrays.asList(emp1,emp2,emp3,emp4,emp5,emp6 );
//Counting on department wise
Map<String , Long> list8 = list.stream().collect(Collectors.groupingBy(Employee:: getDepartment , Collectors.counting()));
System.out.println(list8);
//Avarage
Map<String, Double> map = list.stream().collect(Collectors.groupingBy(Employee:: getGender, Collectors.averagingDouble(Employee:: getSalary)));
System.out.println("Avarge:" +map);
//Highest payment
Optional<Employee> list1 = list.stream().max(Comparator.comparing(Employee::getSalary));
System.out.println("Highest Salry" +list1);
//Avarage age
Map<String, Double> list3 = list.stream().collect(Collectors.groupingBy(Employee::getDepartment , Collectors.averagingDouble(Employee:: getAge)));
System.out.println(list3);
//Senior Employee
Optional<Employee> list4 = list.stream().min(Comparator.comparing(Employee:: getYearOfJoining));
System.out.println(list4);
//Youngest Employee
Optional<Employee> list5 = list.stream().min(Comparator.comparing(Employee:: getAge));
System.out.println("Youngest Employee" +list5);
//Get counting of each department
Map<String, Long> list6 = list.stream().collect(Collectors.groupingBy(Employee:: getDepartment , Collectors.counting()));
System.out.println(list6);
// Number of male and female employee
Map<String , Long> list7 = list.stream().collect(Collectors.groupingBy(Employee:: getGender , Collectors.counting()));
System.out.println(list7);
}
}
|
Beta Was this translation helpful? Give feedback.
-
|
import java.util.ArrayList; long count= e.stream().map(i -> i.getId()).count(); Map<String, Double> avgSalaryOfGenders = e.stream().collect(Collectors.groupingBy(Employee::getGender, Collectors.averagingDouble(Employee::getSalary))); //Get the details of the highest-paid employee in the organization from the given list of employees. //Get the average age of each department in an organization Map<String,Double> avg_age_dept=e.stream().collect(Collectors.groupingBy(Employee::getDepartment,Collectors.averagingDouble(Employee::getAge))); //Find out who is the senior-most employee in the organization in terms of experience. Optional qa=e.stream().max(Comparator.comparing(Employee::getAge)); //Get the details of the youngest employee in the organization. Map<String,Long> ma=e.stream().collect(Collectors.groupingBy(Employee::getDepartment,Collectors.counting())); //Find out the number of male and female employees in the organization. Map<String,Long> count_by_gender1=e.stream().collect(Collectors.groupingBy(Employee::getGender,Collectors.counting())); }`` |
Beta Was this translation helpful? Give feedback.
-
|
import java.util.*; public class Employee{ } |
Beta Was this translation helpful? Give feedback.
-
|
package p1; import java.util.ArrayList; class Employee{ } } |
Beta Was this translation helpful? Give feedback.
-
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
class Employee{
int id;
String name;
int age;
String gender;
String department;
int yearOfJoining;
double salary;
public Employee(int id, String name, int age, String gender, String department, int yearOfJoining, double salary)
{
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.department = department;
this.yearOfJoining = yearOfJoining;
this.salary = salary;
}
public int getId()
{
return id;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String getGender()
{
return gender;
}
public String getDepartment()
{
return department;
}
public int getYearOfJoining()
{
return yearOfJoining;
}
public double getSalary()
{
return salary;
}
@Override
public String toString()
{
return "Id : "+id
+", Name : "+name
+", age : "+age
+", Gender : "+gender
+", Department : "+department
+", Year Of Joining : "+yearOfJoining
+", Salary : "+salary;
}
}
public class EmployeeList {
public static void main(String[] args) {
Employee emp1 = new Employee(3, "Vishal Singhal", 34, "male", "HR", 2013, 300000);
Employee emp2 = new Employee(4, "Amitabh Singh", 35, "male", "Admin", 2014, 500000);
Employee emp3 = new Employee(5, "Vivek Bhati", 28, "male", "Admin", 2017, 500000);
Employee emp4 = new Employee(6, "Vipul Diwan", 34, "male", "Account", 2014, 200000);
Employee emp5 = new Employee(7, "Satish Kumar", 29, "male", "Account", 2017, 75000);
Employee emp6 = new Employee(8, "Geetika Chauhan", 30, "female", "Admin", 2016, 90000);
ArrayList<Employee> employee = new ArrayList<>();
employee.add(emp1);
employee.add(emp2);
employee.add(emp3);
employee.add(emp4);
employee.add(emp5);
employee.add(emp6);
Map<String, Long> count = employee.stream()
.collect(Collectors.groupingBy(Employee::getDepartment, Collectors.counting()));
System.out.println("No of employees in each department " + count);
//average salary of male and female employees
Map<String, Double> avgSalary= employee.stream()
.collect(Collectors.groupingBy(Employee::getGender, Collectors.averagingDouble(Employee::getSalary)));
System.out.println(" average salary of male and female employees: "+avgSalary);
//details of the highest-paid employee in the organization from the given list of employees
Optional<Employee> highestPaidEmployee= employee.stream().max(Comparator.comparingDouble(Employee::getSalary));
System.out.println("Highest paid employee in the organization: "+highestPaidEmployee);
//average age of each department in an organization
Map<String, Double> avgAge= employee.stream()
.collect(Collectors.groupingBy(Employee::getDepartment, Collectors.averagingDouble(Employee::getAge)));
System.out.println("Average age: "+avgAge);
//who is the senior-most employee in the organization in terms of experience
Optional<Employee> seniorMostEmployee= employee.stream().min(Comparator.comparingInt(Employee::getYearOfJoining));
System.out.println("Senior most employee in the organization: "+seniorMostEmployee);
//Get the details of the youngest employee in the organization.
Optional<Employee> youngestEmployee= employee.stream().min(Comparator.comparingDouble(Employee::getAge));
System.out.println("Youngest employee in the organization: "+youngestEmployee);
//Find out the number of male and female employees in the organization
Map<String, Long> countMaleFemaleEmp= employee.stream().collect(Collectors.groupingBy(Employee::getGender, Collectors.counting()));
System.out.println("Male and female employees"+countMaleFemaleEmp);
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
class Employee{
int id;
String name;
int age;
String gender;
String department;
int yearOfJoining;
double salary;
public Employee(int id, String name, int age, String gender, String department, int yearOfJoining, double salary)
{
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.department = department;
this.yearOfJoining = yearOfJoining;
this.salary = salary;
}
public int getId()
{
return id;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String getGender()
{
return gender;
}
public String getDepartment()
{
return department;
}
public int getYearOfJoining()
{
return yearOfJoining;
}
public double getSalary()
{
return salary;
}
@Override
public String toString()
{
return "Id : "+id
+", Name : "+name
+", age : "+age
+", Gender : "+gender
+", Department : "+department
+", Year Of Joining : "+yearOfJoining
+", Salary : "+salary;
}
}
public class EmployeeList {
public static void main(String[] args) {
Employee emp1 = new Employee(3, "Vishal Singhal", 34, "male", "HR", 2013, 300000);
Employee emp2 = new Employee(4, "Amitabh Singh", 35, "male", "Admin", 2014, 500000);
Employee emp3 = new Employee(5, "Vivek Bhati", 28, "male", "Admin", 2017, 500000);
Employee emp4 = new Employee(6, "Vipul Diwan", 34, "male", "Account", 2014, 200000);
Employee emp5 = new Employee(7, "Satish Kumar", 29, "male", "Account", 2017, 75000);
Employee emp6 = new Employee(8, "Geetika Chauhan", 30, "female", "Admin", 2016, 90000);
ArrayList<Employee> employee = new ArrayList<>();
employee.add(emp1);
employee.add(emp2);
employee.add(emp3);
employee.add(emp4);
employee.add(emp5);
employee.add(emp6);
Map<String, Long> count = employee.stream()
.collect(Collectors.groupingBy(Employee::getDepartment, Collectors.counting()));
System.out.println("No of employees in each department " + count);
//average salary of male and female employees
Map<String, Double> avgSalary= employee.stream()
.collect(Collectors.groupingBy(Employee::getGender, Collectors.averagingDouble(Employee::getSalary)));
System.out.println(" average salary of male and female employees: "+avgSalary);
//details of the highest-paid employee in the organization from the given list of employees
Optional<Employee> highestPaidEmployee= employee.stream().max(Comparator.comparingDouble(Employee::getSalary));
System.out.println("Highest paid employee in the organization: "+highestPaidEmployee);
//average age of each department in an organization
Map<String, Double> avgAge= employee.stream()
.collect(Collectors.groupingBy(Employee::getDepartment, Collectors.averagingDouble(Employee::getAge)));
System.out.println("Average age: "+avgAge);
//who is the senior-most employee in the organization in terms of experience
Optional<Employee> seniorMostEmployee= employee.stream().min(Comparator.comparingInt(Employee::getYearOfJoining));
System.out.println("Senior most employee in the organization: "+seniorMostEmployee);
//Get the details of the youngest employee in the organization.
Optional<Employee> youngestEmployee= employee.stream().min(Comparator.comparingDouble(Employee::getAge));
System.out.println("Youngest employee in the organization: "+youngestEmployee);
//Find out the number of male and female employees in the organization
Map<String, Long> countMaleFemaleEmp= employee.stream().collect(Collectors.groupingBy(Employee::getGender, Collectors.counting()));
System.out.println("Male and female employees"+countMaleFemaleEmp);
}
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
You are given the following blueprint for representing a college Employee:
Create six Employee instances:
Create a list of these students called
employeeListand write code to do the following operations:Expected Output:
Beta Was this translation helpful? Give feedback.
All reactions