java 8, lambda , Objects copying: Creating a new list of Normalized objects

739 Views Asked by At

From a REST service, I will get the response as List of Employees. Which may contains multiple addresses for same employee as defined below.

[Employee{empId=1, name='Emp1', address='Emp1 Address1'},
Employee{empId=1, name='Emp1', address='Emp1 Address 2'},
Employee{empId=2, name='Emp2', address='Emp2 Address 1'}]

By creating an another list i.e List<EmployeeNormalized >, the above response needs to be processed in a normalized way, as defined below.

[EmployeeNormalized{empId=1, name='Emp1',
addresses=[Emp1 Address1, Emp1 Address 2]},
EmployeeNormalized{empId=2, name='Emp2', addresses=[Emp2 Address 1]}]

Code snippet:

class Employee {
    private int empId;
    private String name;
    private String address;
    // 50 other properties

    public Employee(int empId, String name, String address) {
        this.empId = empId;
        this.name = name;
        this.address = address;
    }
   // Setters and Getters
}

class EmployeeNormalized {
    private int empId;
    private String name;
    private List<String> addresses;
    // 50 other properties

    public EmployeeNormalized(int empId, String name, List<String> address) {
        this.empId = empId;
        this.name = name;
        this.addresses = address;
    }
   // Setters and Getters
} 

List<EmployeeNormalized > must contain unique employee objects and List<String> in the EmployeeNormalized class should accommodate all the addresses for that employee.

EDIT: Employee class has around 50 properties.

How do I create this normalized form of list?

2

There are 2 best solutions below

4
Chaosfire On BEST ANSWER

Stream solution:

public class Normalize {

    public static void main(String[] args) {
        List<Employee> employees = new ArrayList<>();
        employees.add(new Employee(1, "Emp1", "Address 1"));
        employees.add(new Employee(1, "Emp1", "Address 2"));
        employees.add(new Employee(2, "Emp2", "Address 3"));
        List<EmployeeNormalized> employeeNormalizedList = employees.stream()
                .map(new Function<Employee, EmployeeNormalized>() {

                    private final Map<Integer, EmployeeNormalized> employeeIdMap = new HashMap<>();

                    @Override
                    public EmployeeNormalized apply(Employee employee) {
                        EmployeeNormalized normalized = this.employeeIdMap.computeIfAbsent(employee.getEmpId(),
                                key -> new EmployeeNormalized(employee.getEmpId(), employee.getName(), new ArrayList<>()));
                        normalized.getAddresses().add(employee.getAddress());
                        return normalized;
                    }
                })
                .distinct()
                .collect(Collectors.toList());
        employeeNormalizedList.forEach(System.out::println);
    }
}

Quite complex, need to call distinct to get rid of duplicating instances.

I would go for simple loop:

public class Normalize {

    public static void main(String[] args) {
        List<Employee> employees = new ArrayList<>();
        employees.add(new Employee(1, "Emp1", "Address 1"));
        employees.add(new Employee(1, "Emp1", "Address 2"));
        employees.add(new Employee(2, "Emp2", "Address 3"));

        Map<Integer, EmployeeNormalized> employeeIdMap = new HashMap<>();
        for (Employee employee : employees) {
            EmployeeNormalized normalized = employeeIdMap.computeIfAbsent(employee.getEmpId(), key -> new EmployeeNormalized(employee.getEmpId(), employee.getName(), new ArrayList<>()));
            normalized.getAddresses().add(employee.getAddress());
        }
        List<EmployeeNormalized> employeeNormalizedList = new ArrayList<>(employeeIdMap.values());
        employeeNormalizedList.forEach(System.out::println);
    }
}

Basically both solutions use employee id as unique identifer, and map instances to id. If id is met for the first time, create instance and add address, if there is already instances for this id, get the instance and add address.

Edit: Since computeIfAbsent is undesireable due to many properties, you can add no argument constructor and transfer property values with setters. The best option would be to use mapping library, then you could do it with computeIfAbsent as well.

public class Normalize {

    public static void main(String[] args) {
        List<Employee> employees = new ArrayList<>();
        employees.add(new Employee(1, "Emp1", "Address 1"));
        employees.add(new Employee(1, "Emp1", "Address 2"));
        employees.add(new Employee(2, "Emp2", "Address 3"));

        Map<Integer, EmployeeNormalized> employeeIdMap2 = new HashMap<>();
        for (Employee employee : employees) {
            int id = employee.getEmpId();
            EmployeeNormalized normalized = employeeIdMap2.get(id);
            if (normalized == null) {
                normalized = new EmployeeNormalized();
                normalized.setEmpId(id);
                normalized.setName(employee.getName());
                normalized.setAddresses(new ArrayList<>());
                //set other properties
                //or even better use mapping library to create normalized and transfer property values
                employeeIdMap2.put(id, normalized);
            }
            normalized.getAddresses().add(employee.getAddress());
        }
        List<EmployeeNormalized> employeeNormalizedList2 = new ArrayList<>(employeeIdMap2.values());
        employeeNormalizedList2.forEach(System.out::println);
    }
}
0
Nowhere Man On

This task can be resolved with Stream API, providing that there is some wrapper class/record to represent a key (employeeId and employeeName). Even plain ArrayList<Object> could be used for this purpose but with introduction of record since Java 16 it's better to use them.

The solution itself is pretty straightforward: use Collectors.groupingBy to create a key, Collectors.mapping to build a list of addresses per employee, and finally join the key (employeeId and employeeName) and the value list of addresses in the EmployeeNormalized:

//
List<Employee> employees = .... ; // build employee list
List<EmployeeNormalized> normEmployees = employees
    .stream()
    .collect(Collectors.groupingBy(
        emp -> Arrays.asList(emp.getEmpId(), emp.getName()),
        LinkedHashMap::new, // maintain insertion order
        Collectors.mapping(Employee::getAddress, Collectors.toList())
    )) // Map<List<Object>, List<String>>
    .entrySet()
    .stream()
    .map(e -> new EmployeeNormalized(
        ((Integer) e.getKey().get(0)).intValue(), // empId
        (String) e.getKey().get(1),               // name
        e.getValue()
    ))
    .collect(Collectors.toList());

Use of record allows to maintain typesafe keys without extra casting. Record can be replaced with a small wrapper class to represent a key with hashCode / equals methods overridden because the instances of this class are used as keys in an intermediate map.

// Java 16+
record EmpKey(int empId, String name) {}

List<EmployeeNormalized> normEmployees = employees
    .stream()
    .collect(Collectors.groupingBy(
        emp -> new EmpKey(emp.getEmpId(), emp.getName()),
        LinkedHashMap::new, // maintain insertion order
        Collectors.mapping(Employee::getAddress, Collectors.toList())
    )) // Map<List<Object>, List<String>>
    .entrySet()
    .stream()
    .map(e -> new EmployeeNormalized(
        e.getKey().empId(), e.getKey().name(), e.getValue()
    ))
    .collect(Collectors.toList());