简体   繁体   中英

Iterating through multiple lists

I have two seperate lists from a DTO i created, the two lists are containing different set of data ie in first list lets say there are some 'userloginname' along with 2nd last updated 'status'. In the 2nd List i have 'userloginname' with all the status record(not just the 2nd last updated status).

Is this possible if want to take each 'userloginname' from List1 and compare with each 'userloginname' in the list2 with some condition.?

thank you

Correct me if I'm misunderstanding, but how about something like this:

for (int i = 0 ; i < list1.size() ; i++) {
    // Do something with "list1.get(i)" and "list2.get(i)"
}

With this you can compare items at a given index in list1 with items at the corresponding index in list2 . If you want to compare every list1 member with every other list2 member, you could do something like:

for (int i = 0 ; i < list1.size() ; i++)
    for (int j = 0 ; j < list2.size() ; j++) {
        // Do something with "list1.get(i)" and "list2.get(j)"
    }

Convert one of the lists to a map using 'userloginname' as a key.

List<LastStatusRecord> lastStatusRecords = dto.getLastStatusRecords();
List<StatusRecord> statusRecords = dto.getStatusRecords();

Map<String, FullRecord> statusRecordIndex = new HashMap<String, StatusRecord>();
for (StatusRecord statusRecord : statusRecords) {
  statusRecordIndex.put(statusRecord.getUserLoginName(), statusRecord);
}
for (LastStatusRecord lastStatusRecord : lastStatusRecords) {
  StatusRecord statusRecord = statusRecordIndex.get(lastStatusRecord.getUserLoginName());
  // place the condition 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