javajava-stream

java stream to set the values to a list from another list


I have 2 arraylist i.e. EmployeeList and AdddressList. I need to add pincode to employee whenever a matching id is found in addresslist in java. Could you please help me with a solution in java8 with single iteration. I cannot iterate 2 arraylist as it will be order of n*n.

Could you please find the below code snippet.We need to add pincode to same the employeelist where id is matching from Addresslist.We need to the existing values of employeelist as well.

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

class Address{
    private int id;
    private String pinCode;

    public Address(int id, String pinCode) {
        this.id = id;
        this.pinCode = pinCode;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getPinCode() {
        return pinCode;
    }

    public void setPinCode(String pinCode) {
        this.pinCode = pinCode;
    }

    @Override
    public String toString() {
        return "Address{" +
                "id=" + id +
                ", pinCode='" + pinCode + '\'' +
                '}';
    }
}
class Employee {

    private String name;
    private int id;
    private String department;
    private int age;
    private String gender;
    private int date;
    private double salary;
    private String pinCode;
    public Employee(int id, String name, int age,String gender,String department,int date,double salary,
    String pinCode) {
        this.name = name;
        this.id = id;
        this.age=age;
        this.gender=gender;
        this.department = department;
        this.date=date;
        this.salary=salary;
        this.pinCode=pinCode;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getDate() {
        return date;
    }

    public void setDate(int date) {
        this.date = date;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public String getPincode() {
        return pinCode;
    }

    public void setPincode(String pinCode) {
        this.pinCode = pinCode;
    }

    @Override
    public String  toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", id=" + id +
                ", department='" + department + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                ", date=" + date +
                ", salary=" + salary +
                '}';
    }
}
public class Program{
    public static void main(String[] args){
        List<Employee> employeeList = new ArrayList<Employee>();

        employeeList.add(new Employee(111, "Jiya Brein", 32, "Female", "HR", 2011, 25000.0,null));
        employeeList.add(new Employee(122, "Paul Niksui", 25, "Male", "Sales And Marketing", 2015, 13500.0,null));
        employeeList.add(new Employee(133, "Martin Theron", 29, "Male", "Infrastructure", 2012, 18000.0,null));
        employeeList.add(new Employee(144, "Murali Gowda", 28, "Male", "Product Development", 2014, 32500.0,null));
        employeeList.add(new Employee(155, "Nima Roy", 27, "Female", "HR", 2013, 22700.0,null));
        employeeList.add(new Employee(166, "Iqbal Hussain", 43, "Male", "Security And Transport", 2016, 10500.0,null));
        List<Address> addressList = new ArrayList<>();
        addressList.add(new Address(111,"700141"));
        addressList.add(new Address(122,"700111"));
        


    }
}

Solution

  • Using a HashMap of pin codes (as @Michael already suggested) produces two iterations, achieving a linear time complexity of O(n).

    First, create a HashMap with predefined initialCapacity to avoid resizing of the underlying table (array). Then, use addressList to populate it with ids as keys and pinCodes as values. The get() and put() methods of HashMap have constant time performance (under perfect conditions).

    Finally, iterate over employeeList and set the pinCode values retrieved from this map.

    final float LOAD_FACTOR = 0.75f;
    final Map<Integer, String> idPinMap = new HashMap<>(
            (int) (addressList.size() / LOAD_FACTOR + 1), LOAD_FACTOR);
    
    addressList.stream().forEach(a -> idPinMap.put(a.getId(), a.getPinCode()));
    employeeList.stream().forEach(e -> e.setPincode(idPinMap.get(e.getId())));