【问题标题】:Comparing nested lists within a parent list structure for values in specific indices in java比较父列表结构中的嵌套列表以获取 java 中特定索引中的值
【发布时间】:2019-12-21 09:09:35
【问题描述】:

给定以下格式的列表:

List>行

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

如果存在帐户 ID 和员工 ID 重复的行,可以执行操作吗?

【问题讨论】:

  • List < String, String, BigInteger, String, String, String, String, String, String > 是如何真正构造的?你创建了一个新类来照顾< String, String, BigInteger, String, String, String, String, String, String >
  • 是的,它是一个列表的列表。每个列表都是一个特定的记录。我试图找出检查employeeID 和accountID 相同的条目的最佳方法,以处理相同操作的重复操作的批处理。然而,它本身似乎有一个 groovy 类来处理直接从数据库中提取相关数据。我想直接创建这些对象的列表可能会更好? @lbald

标签: java list collections iteration nested-lists


【解决方案1】:

我假设我得到了结构的含义如下:

List< Row >,其中Row 是您为管理List<String, String, BigInteger, String, String, String, String, String, String> 而创建的类。

在这种情况下,您可以覆盖此类中的方法 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;
    }
}

然后在您的主列表中,您可以应用其中一种算法来查找重复项。如 [https://crunchify.com/java-how-to-find-duplicate-elements-from-list/]。一个例子可能是:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-04
    • 2020-05-16
    • 1970-01-01
    • 2019-08-12
    • 2023-03-12
    • 2020-03-14
    • 1970-01-01
    相关资源
    最近更新 更多