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

Java Stream Grouping and Sorting Examples

The document contains Java code snippets for analyzing employee data, including counting employees by gender and department, calculating average age and salary, and identifying the highest paid and oldest employees. It also includes functionality to filter employees based on joining year and age, as well as partitioning employees by age. The code utilizes Java Streams and Collectors for data processing and outputting results to the console.

Uploaded by

Andrei Tapia
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)
14 views3 pages

Java Stream Grouping and Sorting Examples

The document contains Java code snippets for analyzing employee data, including counting employees by gender and department, calculating average age and salary, and identifying the highest paid and oldest employees. It also includes functionality to filter employees based on joining year and age, as well as partitioning employees by age. The code utilizes Java Streams and Collectors for data processing and outputting results to the console.

Uploaded by

Andrei Tapia
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

**Section 2: Counting Male and Female Employees**

```java
Map<String, Long> countByGender = [Link]()
.collect([Link](Employee::getGender, [Link]()));
[Link]("Count by Gender: " + countByGender);
```

**Section 3: Listing Departments**


```java
[Link]()
.map(Employee::getDepartment)
.distinct()
.forEach([Link]::println);
```

**Section 4: Average Age of Employees by Gender**


```java
Map<String, Double> avgAgeByGender = [Link]()
.collect([Link](Employee::getGender,
[Link](Employee::getAge)));
[Link]("Average Age by Gender: " + avgAgeByGender);
```

**Section 5: Highest Paid Employee**


```java
Optional<Employee> highestPaidEmployee = [Link]()
.collect([Link]([Link](Employee::getSalary)));
[Link](employee -> [Link]("Highest Paid
Employee: " + employee));
```

**Section 6: Employees Joined After 2015**


```java
[Link]()
.filter(e -> [Link]() > 2015)
.map(Employee::getName)
.forEach([Link]::println);
```

**Section 7: Number of Employees in Each Department**


```java
Map<String, Long> countByDepartment = [Link]()
.collect([Link](Employee::getDepartment,
[Link]()));
[Link]("Count by Department: " + countByDepartment);
```

**Section 8: Average Salary by Department**


```java
Map<String, Double> avgSalaryByDepartment = [Link]()
.collect([Link](Employee::getDepartment,
[Link](Employee::getSalary)));
[Link]("Average Salary by Department: " + avgSalaryByDepartment);
```

**Section 9: Youngest Male Employee in Product Development**


```java
Optional<Employee> youngestMaleInProductDev = [Link]()
.filter(e -> [Link]().equals("Male") && [Link]().equals("Product
Development"))
.min([Link](Employee::getAge));
[Link](employee -> [Link]("Youngest Male in
Product Development: " + employee));
```

**Section 10: Most Experienced Employee**


```java
Optional<Employee> mostExperiencedEmployee = [Link]()
.sorted([Link](Employee::getYearOfJoining))
.findFirst();
[Link](employee -> [Link]("Most Experienced
Employee: " + employee));
```

**Section 11: Gender Count in Sales and Marketing**


```java
Map<String, Long> genderCountInSalesMarketing = [Link]()
.filter(e -> [Link]().equals("Sales and Marketing"))
.collect([Link](Employee::getGender, [Link]()));
[Link]("Gender Count in Sales and Marketing: " +
genderCountInSalesMarketing);
```

**Section 12: Average Salary by Gender**


```java
Map<String, Double> avgSalaryByGender = [Link]()
.collect([Link](Employee::getGender,
[Link](Employee::getSalary)));
[Link]("Average Salary by Gender: " + avgSalaryByGender);
```

**Section 13: List of Employees by Department**


```java
Map<String, List<String>> employeesByDepartment = [Link]()
.collect([Link](Employee::getDepartment,
[Link](Employee::getName, [Link]())));
[Link]("Employees by Department: " + employeesByDepartment);
```

**Section 14: Organization's Average and Total Salary**


```java
DoubleSummaryStatistics salaryStats = [Link]()
.collect([Link](Employee::getSalary));
[Link]("Average Salary: " + [Link]());
[Link]("Total Salary: " + [Link]());
```

**Section 15: Partitioning Employees by Age**


```java
Map<Boolean, List<Employee>> partitionedByAge = [Link]()
.collect([Link](e -> [Link]() > 25));
[Link]("Employees 25 or Younger: " + [Link](false));
[Link]("Employees Older than 25: " + [Link](true));
```

**Section 16: Oldest Employee Details**


```java
Optional<Employee> oldestEmployee = [Link]()
.max([Link](Employee::getAge));
[Link](employee -> [Link]("Oldest Employee: " +
employee));
```

Common questions

Powered by AI

The technique applied involves using Java Streams to group employees by department and then count the number of employees in each department using Collectors.counting().

The highest-paid employee is identified using Java Streams with Collectors.maxBy() and Comparator.comparingDouble() to compare employees' salaries and select the employee with the maximum salary .

The method used to find the average age of employees by gender utilizes Java Streams to group employees by gender, and then it calculates the average age for each gender using Collectors.averagingInt().

The approach used is Java Streams with Collectors.partitioningBy(), which divides employees into two groups: those older than 25 and those 25 or younger. This partitioning is useful for demographic analyses, targeting specific age groups for benefits or training programs .

The organization identifies the most experienced employee by sorting the employee list based on the year of joining in ascending order and selecting the first employee, which represents the one who joined the earliest .

The organization uses Java Streams to count male and female employees by collecting and grouping the employee data by gender and then counting each group using Collectors.counting().

The company lists all distinct departments by streaming the employee list, mapping the department names, filtering for distinct values using distinct(), and then printing each unique department .

The organization determines gender distribution in Sales and Marketing by filtering employees belonging to that department, then using Java Streams to group and count them by gender using Collectors.groupingBy() and Collectors.counting().

The criteria include filtering employees who are male and belong to the Product Development department, then finding the employee with the minimum age using Comparator.comparingInt() within a Java Stream .

The average salary by department is calculated by streaming the employee list, grouping employees by their department, and then using Collectors.averagingDouble() to compute the average salary for each department .

You might also like