0% found this document useful (0 votes)
23 views3 pages

Employee Data Analysis with Streams

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views3 pages

Employee Data Analysis with Streams

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

class Employee

{
int id;
String name;
int age;
String gender;
String department;
int yearOfJoining;
double salary;

// constructor
// getters and setters
}

==============================================================
List<Employee> employeeList = new ArrayList<Employee>();

[Link](new Employee(1, "Jhansi", 32, "Female", "HR", 2011, 25000.0));


[Link](new Employee(2, "Smith", 25, "Male", "Sales", 2015, 13500.0));
[Link](new Employee(3, "David", 29, "Male", "Infrastructure", 2012,
18000.0));
[Link](new Employee(4, "Orlen", 28, "Male", "Development", 2014,
32500.0));
[Link](new Employee(5, "Charles", 27, "Male", "HR", 2013, 22700.0));
[Link](new Employee(6, "Cathy", 43, "Male", "Security", 2016, 10500.0));
[Link](new Employee(7, "Ramesh", 35, "Male", "Finance", 2010, 27000.0));
[Link](new Employee(8, "Suresh", 31, "Male", "Development", 2015,
34500.0));
[Link](new Employee(9, "Gita", 24, "Female", "Sales", 2016, 11500.0));
[Link](new Employee(10, "Mahesh", 38, "Male", "Security", 2015,
11000.5));
[Link](new Employee(11, "Gouri", 27, "Female", "Infrastructure", 2014,
15700.0));
[Link](new Employee(12, "Nithin", 25, "Male", "Development", 2016,
28200.0));
[Link](new Employee(13, "Swathi", 27, "Female", "Finance", 2013,
21300.0));
[Link](new Employee(14, "Buttler", 24, "Male", "Sales", 2017, 10700.5));
[Link](new Employee(15, "Ashok", 23, "Male", "Infrastructure", 2018,
12700.0));
[Link](new Employee(16, "Sanvi", 26, "Female", "Development", 2015,
28900.0));

1. How many male and female employees are there in the organization ?

Map<String, Long> map1 =


[Link]().collect([Link](Employee::getGender,
[Link]()));
[Link](map1);

2. Print the name of all departments in the organization ?

[Link]()
.map(Employee::getDepartment)
.distinct()
.forEach(name -> [Link](name));
3. What is the average age of male and female employees ?

Map<String, Double> map = [Link]()


.collect([Link](Employee::getGender,
[Link](Employee::getAge)));
[Link](map);

4. Get the details of highest paid employee in the organization ?

Optional<Employee> optional = [Link]()


.collect([Link]([Link](Employee::ge
tSalary)));

if([Link]()) {
Employee employee = [Link]();
[Link](employee);
}

5. Get the names of all employees who have joined after 2015 ?

[Link]()
.filter(e -> [Link] > 2015)
.map(e -> [Link])
.forEach(name -> [Link](name));

6. Count the number of employees in each department ?

Map<String, Long> map = [Link]()


.collect([Link]
y(Employee::getDepartment, [Link]()));
[Link](map);

7. What is the average salary of each department ?

Map<String, Double> map = [Link]()


.collect([Link](Employee::getDepartment,
[Link](Employee::getSalary)));
[Link](map);

8. Get the details of youngest male employee in the Development department ?

Optional<Employee> optional = [Link]()


.filter(e -> [Link]().equals("Male") &&
[Link]().equals("Development"))
.min([Link](Employee::getAge));

if([Link]()) {
[Link]([Link]());
}

9. Who has the most working experience in the organization ?

Optional<Employee> optional = [Link]()


.collect([Link]([Link](Employee::getYearO
fJoining)));

if([Link]()) {
[Link]([Link]());
}

10. How many male and female employees are there in the Sales team ?

Map<String, Long> map = [Link]()


.filter(e ->
[Link]().equals("Sales"))
.collect([Link]
y(Employee::getGender, [Link]()));

[Link](map);

11. What is the average salary of male and female employees ?

12. List down the names of all employees in each department ?

13. What is the average salary and total salary of the whole organization ?

14. Separate the employees who are younger or equal to 25 years from those
employees who are older than 25 years ?

15. Who is the oldest employee in the organization?

Common questions

Powered by AI

To ascertain the different departments available, use the stream API to map the department names and then apply a distinct filter to extract unique departments. This can be achieved with: employeeList.stream().map(Employee::getDepartment).distinct().forEach(System.out::println). This lists all unique department names available in the company .

The employee with the most working experience can be identified by finding the one with the earliest year of joining. This can be achieved with: employeeList.stream().min(Comparator.comparing(Employee::getYearOfJoining)). The result is the employee who joined the earliest, hence having the most working experience within the organization .

To find the average salary of employees in each department using the Java Stream API, you can group employees by their department and then calculate the average salary within each group. This can be implemented using: employeeList.stream().collect(Collectors.groupingBy(Employee::getDepartment, Collectors.averagingDouble(Employee::getSalary))). This will return a map where each key is a department and each value is the average salary of employees in that department .

We can determine the number of male and female employees in each department by first filtering the employees based on their department and then grouping them by gender. This can be achieved using the stream API in Java with the following logic: emps.stream().collect(Collectors.groupingBy(Employee::getDepartment, Collectors.groupingBy(Employee::getGender, Collectors.counting()))). This approach allows us to map each department to a secondary map that counts the male and female employees within that department .

You can separate employees based on age using Java's stream partitioning feature: employeeList.stream().collect(Collectors.partitioningBy(e -> e.getAge() <= 25)). This method partitions employees into two groups: one for those who are 25 years or younger and another for those older than 25. It provides a clear division of employees based on the specified age criteria .

To list down departments with the number of employees in each, we can use Java streams to group the employees by their department and then count them. This can be done using: employeeList.stream().collect(Collectors.groupingBy(Employee::getDepartment, Collectors.counting())). This method collects the data into a map where each key is a department name, and the value is the count of employees in that department .

To determine the average and total salary for the entire organization, sum the salaries of all employees and divide by the count of employees for the average salary. For the total salary, use the sum of all employee salaries. This involves: double totalSalary = employeeList.stream().mapToDouble(Employee::getSalary).sum(); double averageSalary = totalSalary / employeeList.size(); This provides the aggregated financial insights into the organization's salary structure .

To determine the number of male and female employees in the Sales team, filter the employee list for those in the Sales department, then group by gender and count. This process is: employeeList.stream().filter(e -> e.getDepartment().equals("Sales")).collect(Collectors.groupingBy(Employee::getGender, Collectors.counting())). This setup provides a count breakdown of male and female employees in the Sales team .

To identify the youngest male employee in the Development department, filter the employee list for males in the Development department, and then find the employee with the minimum age using a comparator. This can be achieved with: employeeList.stream().filter(e -> e.getGender().equals("Male") && e.getDepartment().equals("Development")).min(Comparator.comparing(Employee::getAge)). This will return an Optional<Employee> containing the youngest male employee in the Development department if present .

The highest paid employee can be identified by finding the employee with the maximum salary using a comparator. This can be achieved with: employeeList.stream().max(Comparator.comparingDouble(Employee::getSalary)). This returns an Optional<Employee> object, which contains the employee with the highest salary if present .

You might also like