anandkk
anandkk

Reputation: 71

Iterating through multiple lists

I have two seperate lists from a DTO i created, the two lists are containing different set of data i.e 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

Upvotes: 0

Views: 178

Answers (2)

Sami Korhonen
Sami Korhonen

Reputation: 1303

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
}

Upvotes: 0

arshajii
arshajii

Reputation: 129477

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)"
    }

Upvotes: 1

Related Questions