0% found this document useful (0 votes)
3 views

Java_Streams_Coding_Questions_Sections

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)
3 views

Java_Streams_Coding_Questions_Sections

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
You are on page 1/ 3

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

```java
Map<String, Long> countByGender = employeeList.stream()
.collect(Collectors.groupingBy(Employee::getGender, Collectors.counting()));
System.out.println("Count by Gender: " + countByGender);
```

**Section 3: Listing Departments**


```java
employeeList.stream()
.map(Employee::getDepartment)
.distinct()
.forEach(System.out::println);
```

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


```java
Map<String, Double> avgAgeByGender = employeeList.stream()
.collect(Collectors.groupingBy(Employee::getGender,
Collectors.averagingInt(Employee::getAge)));
System.out.println("Average Age by Gender: " + avgAgeByGender);
```

**Section 5: Highest Paid Employee**


```java
Optional<Employee> highestPaidEmployee = employeeList.stream()
.collect(Collectors.maxBy(Comparator.comparingDouble(Employee::getSalary)));
highestPaidEmployee.ifPresent(employee -> System.out.println("Highest Paid
Employee: " + employee));
```

**Section 6: Employees Joined After 2015**


```java
employeeList.stream()
.filter(e -> e.getYearOfJoining() > 2015)
.map(Employee::getName)
.forEach(System.out::println);
```

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


```java
Map<String, Long> countByDepartment = employeeList.stream()
.collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.counting()));
System.out.println("Count by Department: " + countByDepartment);
```

**Section 8: Average Salary by Department**


```java
Map<String, Double> avgSalaryByDepartment = employeeList.stream()
.collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.averagingDouble(Employee::getSalary)));
System.out.println("Average Salary by Department: " + avgSalaryByDepartment);
```

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


```java
Optional<Employee> youngestMaleInProductDev = employeeList.stream()
.filter(e -> e.getGender().equals("Male") && e.getDepartment().equals("Product
Development"))
.min(Comparator.comparingInt(Employee::getAge));
youngestMaleInProductDev.ifPresent(employee -> System.out.println("Youngest Male in
Product Development: " + employee));
```

**Section 10: Most Experienced Employee**


```java
Optional<Employee> mostExperiencedEmployee = employeeList.stream()
.sorted(Comparator.comparingInt(Employee::getYearOfJoining))
.findFirst();
mostExperiencedEmployee.ifPresent(employee -> System.out.println("Most Experienced
Employee: " + employee));
```

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


```java
Map<String, Long> genderCountInSalesMarketing = employeeList.stream()
.filter(e -> e.getDepartment().equals("Sales and Marketing"))
.collect(Collectors.groupingBy(Employee::getGender, Collectors.counting()));
System.out.println("Gender Count in Sales and Marketing: " +
genderCountInSalesMarketing);
```

**Section 12: Average Salary by Gender**


```java
Map<String, Double> avgSalaryByGender = employeeList.stream()
.collect(Collectors.groupingBy(Employee::getGender,
Collectors.averagingDouble(Employee::getSalary)));
System.out.println("Average Salary by Gender: " + avgSalaryByGender);
```

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


```java
Map<String, List<String>> employeesByDepartment = employeeList.stream()
.collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.mapping(Employee::getName, Collectors.toList())));
System.out.println("Employees by Department: " + employeesByDepartment);
```

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


```java
DoubleSummaryStatistics salaryStats = employeeList.stream()
.collect(Collectors.summarizingDouble(Employee::getSalary));
System.out.println("Average Salary: " + salaryStats.getAverage());
System.out.println("Total Salary: " + salaryStats.getSum());
```

**Section 15: Partitioning Employees by Age**


```java
Map<Boolean, List<Employee>> partitionedByAge = employeeList.stream()
.collect(Collectors.partitioningBy(e -> e.getAge() > 25));
System.out.println("Employees 25 or Younger: " + partitionedByAge.get(false));
System.out.println("Employees Older than 25: " + partitionedByAge.get(true));
```

**Section 16: Oldest Employee Details**


```java
Optional<Employee> oldestEmployee = employeeList.stream()
.max(Comparator.comparingInt(Employee::getAge));
oldestEmployee.ifPresent(employee -> System.out.println("Oldest Employee: " +
employee));
```

You might also like