简体   繁体   中英

Comparing nested lists within a parent list structure for values in specific indices in java

Given a list of lists in the following format:

List < List < String, String, BigInteger, String, String, String, String, String, String > > rows

what would be the best way to iterate through each list searching for duplicate values for [1] (string for employee ID) and [2] (big integer for account ID) indices that match any other row's values for [1] and [2] in a list

so that if there are rows with duplicates of both account id's and employee id's an action can be done?

I'm supposing I got the meaning of the structure as follows:

List< Row > , where Row is the class you created to manage List<String, String, BigInteger, String, String, String, String, String, String> .

In this case you could override the method equals() in this class to match your "duplicated" values.

class Row {
    ...
    @Override
    public boolean equals(Object o) {
        if (o instanceof Row) {
            return account.equals(((Row)o).getAccount())
                && employee.equals((Row)o).getEmployee());
        }
        return false;
    }
}

Then in your main list, you can apply one of the algorithms to find duplications. As in [ https://crunchify.com/java-how-to-find-duplicate-elements-from-list/] . One example could be:

void processDuplicates (List<Row> list) {
    Set<Row> already = new HashSet<>();
    for (Row row : list) {
        if (already.contains(row)) {
            // row is duplicated
            // do something
        } else {
            already.add(row);
        }
    }
}

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