简体   繁体   English

比较父列表结构中的嵌套列表以获取Java中特定索引中的值

[英]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 列表<列表<字符串,字符串,BigInteger,字符串,字符串,字符串,字符串,字符串,字符串>>行

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 遍历每个列表的最佳方法是什么,以搜索与[1]和[2]的任何其他行的值匹配的[1](员工ID的字符串)和[2](帐户ID的大整数)索引的重复值]在列表中

so that if there are rows with duplicates of both account id's and employee id's an action can be done? 这样一来,如果有行同时存在帐户ID和员工ID的重复,可以采取措施吗?

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> . List< Row > ,其中Row是您创建的用于管理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. 在这种情况下,您可以重写此类中的equals()方法以匹配您的“重复”值。

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/] . 如[ 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);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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