Answer:
Introduction:
This question assesses your understanding of Java programming concepts, particularly the Stream API, and your ability to solve a common problem efficiently.
Solution Overview:
To solve this problem, we’ll utilize the Java Stream API to process a list of employees and identify duplicate names. Here’s a step-by-step approach:
- Define Employee Class:
We’ll start by defining anEmployee
class with fields forname
andid
. - Create a List of Employees:
We’ll create a list ofEmployee
objects containing sample data. - Group Employees by Name and Count Occurrences:
We’ll use the Stream API’sgroupingBy
collector to group employees by their names and count the occurrences of each name. - Filter Duplicate Names:
Next, we’ll filter the grouped names to include only those with occurrences greater than 1, indicating duplicates. - Collect Duplicate Names into a List:
Finally, we’ll collect the duplicate names into a list for further processing or display.
Code Implementation:
Here’s the Java code implementing the solution:
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
class Employee {
private String name;
private int id;
public Employee(String name, int id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
}
public class Main {
public static void main(String[] args) {
List<Employee> employees = Arrays.asList(
new Employee("Java", 1),
new Employee("Python", 2),
new Employee("Java", 3),
new Employee("Go", 4),
new Employee("Python", 5)
);
// Group employees by name and count occurrences
Map<String, Long> nameCountMap = employees.stream()
.collect(Collectors.groupingBy(Employee::getName, Collectors.counting()));
// Filter names with count greater than 1 (i.e., duplicates)
List<String> duplicateNames = nameCountMap.entrySet().stream()
.filter(entry -> entry.getValue() > 1)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
System.out.println("Duplicate names: " + duplicateNames);
}
}
// OUTPUT: Duplicate names: [Java, Python]
JavaIn this example:
- We define an
Employee
class withname
andid
fields. - In the
Main
class, we create a list of employees. - We use the Stream API to group employees by their names and count occurrences of each name.
- Then, we filter out names with occurrences greater than 1 (i.e., duplicates).
- Finally, we collect the duplicate names into a list and print them out.
We can modify the output to return a map where the keys are the duplicate names and the values are the counts of how many times each name appears. Here’s the updated code:
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
class Employee {
private String name;
private int id;
public Employee(String name, int id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
}
public class Main {
public static void main(String[] args) {
List<Employee> employees = Arrays.asList(
new Employee("Angad", 1),
new Employee("Java", 2),
new Employee("Angad", 3),
new Employee("Java", 4),
new Employee("Python", 5)
);
// Group employees by name and count occurrences
Map<String, Long> nameCountMap = employees.stream()
.collect(Collectors.groupingBy(Employee::getName, Collectors.counting()));
// Filter names with count greater than 1 (i.e., duplicates)
Map<String, Long> duplicateNamesMap = nameCountMap.entrySet().stream()
.filter(entry -> entry.getValue() > 1)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
System.out.println("Duplicate names: " + duplicateNamesMap);
}
}
// OUTPUT: Duplicate names: {Java=2, Angad=3}
JavaIn this updated version, duplicateNamesMap
contains the names of employees that appear more than once as keys, and the number of times each name appears as values. This map is then printed to the console.
Conclusion:
In this solution, we’ve demonstrated the use of Java Stream API to efficiently find duplicate names in a list of employees. By leveraging stream operations like groupingBy
and filter
, we’ve provided a concise and readable solution to the given problem.