ANGADJAVA Code Hub Bytes,Java Interview Question Finding Duplicate Names in a List of Employees Using Stream API

Finding Duplicate Names in a List of Employees Using Stream API

0 Comments 7:32 pm

interview, job, icon-1018332.jpg


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:

  1. Define Employee Class:
    We’ll start by defining an Employee class with fields for name and id.
  2. Create a List of Employees:
    We’ll create a list of Employee objects containing sample data.
  3. Group Employees by Name and Count Occurrences:
    We’ll use the Stream API’s groupingBy collector to group employees by their names and count the occurrences of each name.
  4. Filter Duplicate Names:
    Next, we’ll filter the grouped names to include only those with occurrences greater than 1, indicating duplicates.
  5. 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]
Java

In this example:

  • We define an Employee class with name and id 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}
Java

In 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.


Leave a Reply

Your email address will not be published. Required fields are marked *