简体   繁体   中英

How to efficently join two lists on id in Java 7?

I have a problem with getting two lists of objects and joining these together efficently to essentially create a third datamodel that is returned in an endpoint on my api. One of the issues is that i get a list of entities [Employees] from one database and then i get another list of entities [ClockedIn] from another database so it looks like this:

class Employee {
  private Integer employeeID;
  private String firstName;
  private String lastName;
  etc...

  //corresponding getters and setters
}

and

class ClockedIn {
  private Integer employeeID;
  private Date timeStamp;
  etc...

//corresponding getters and setters

} 

For joining now i do it like this:

List<Employee> getEmployees = repository.getAll();
List<ClockedIn> getClockedIn = repository.getAll();
List<JoinedDataModel> joinedDataModelList = new ArrayList<JoinedDataModel>();

for(Employee emp : getEmployees) {
    JoinedDataModel joinDataModel = new JoinedDataModel();
    int currentId = empt.getEmployeeId();
    List<Dates> missingClockedIn = new ArrayList<Dates>();
    for(ClockedIn ci : getClockedIn) {
        if(currentId == ci.getEmployeeId) {
            // add together to the third datamodel
            joinDataModel.setId();
            if(missingDate(ci.getTimeStamp()) {
                missingClockedIn.add(ci.getTimeStamp());
            }
        }
        joinDataModel.setMissingClockedIn(missingClockedIn);
    }
}
joinedDataModelList.add(joinDataModel);

I have about 70k items in the ClockedIn-list and 1400 of the Employee-List and it takes about 15 seconds to compute, but should be taken into account that i need to retrieve data from two diffrent databases. However if i add 10k to the ClockedIn-list the server gets timed out, Im not that knowledgeable with Big O notation but i think that the way i have it set up now it is quite performance heavy because of the nested loops im thinking it is O(n2)?

Is there an more efficent approach i could take? One thing i think would make it smoother is if the two lists were joined together before doing the mapping to the third datamodel but i'm stumped on how i could do it in a more elegant way.

Any input on my implementation above would be greatly appreciated!

Thanks!


*Disclaimer, everything is pseudo-code so any syntax error or the like found in the code posted here arent relevant for my issue.

The basic idea is that you're going to run through the ClockedIn collection and organize them into ArrayLists which can be easily accessed with the EmployeeId value. This is done using a HashMap object which can use the EmployeeId as a Key for rapid access. Example:

//Class declaration for example use
class Employee {
    int employeeId;
    //Plus any other info
    Employee(int employeeId) {
        this.employeeId = employeeId;
    }
}

class ClockedIn{
    int employeeId;
    //Plus any other info
    ClockedIn(int employeeId) {
        this.employeeId = employeeId;
    }
}

//Data creation for example use
private List<Employee> getEmployeeData() {
    List<Employee> data = new ArrayList<>();
    for(int i = 0; i < 50; i++) {
        data.add(new Employee(i));
    }
    return data;
}

private List<ClockedIn> getClockedInData() {
    List<ClockedIn> data = new ArrayList<>();
    for(int i = 0; i < 1000; i++) {
        data.add(new ClockedIn(i % 50));
    }
    return data;
}

//Actual hashmapping and combining
private void hashmap() {
    HashMap<Integer, ArrayList<ClockedIn>> map = new HashMap<>();

    for(ClockedIn data : getClockedInData()) {
        if(map.get(data.employeeId) == null)
            map.put(data.employeeId, new ArrayList<ClockedIn>());
        map.get(data.employeeId).add(data);
    }

    for(Employee data : getEmployeeData()) {
        ArrayList<ClockedIn> relatedClockData = map.get(data.employeeId);
        //Combine the Employee data with the related ClockedIn data here
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM