树中的第一个元素变为空?番石榴树基表

发布于 2024-11-30 14:55:00 字数 1499 浏览 2 评论 0原文

所以我遇到了 Guava TreeBasedTable 的问题(如果你不熟悉,它是一棵基于一对键访问其元素的树),在过去的一周里一直很难弄清楚。我将尽力解释,删除多余的代码:

TreeBasedTable<RowValue, Data, Result> results = TreeBasedTable.create();    
for (Data d : data.getData()) {
            for (Operation o: data.getOperations()) {
                Result r = o.calculate(...);      
                    results.put(r.rowValue, d, r);
    }
}

基本上,我迭代我拥有的一些数据,进行一些计算,并将结果粘贴在表中。但奇怪的是,当我尝试访问元素时。如果我简单地迭代它们,如下所示:

for(Result r : results.values()){
    System.out.println(r);
}

一切正常。但是,如果我尝试按如下方式访问它们:

for(RowValue row : results.rowKeySet()){
    for(Data d : results.columnKeySet()){
        System.out.println(results.get(row, d));
    }
}

第一个元素不知何故为空。然而,如果树的大小为 1,它就可以正常工作。难道这里发生了一些我不明白的关于树木的事情吗?抱歉问了这么长的问题,我希望它很清楚。

::编辑:: 传递到树中的第一个值始终为非空。然而,当树的大小达到 3 时,它会从非空变为空。抱歉,如果不清楚我的问题是什么。 根据要求,这里是带有实际键的实际代码:

public void createResults(Options options, MeasuredData data, ArrayList method) {

private TreeBasedTable<MethodDescriptor, Compound, Result> results = TreeBasedTable.create();

for (Compound c : data.getCompounds()) {
    for (Method method : methods) {
        ArrayList<Result> calcResults = method.calculate(c, options);
           for (Result r : calcResults) {                   
               results.put(r.getMethod(), c, r);
           }
     }
}

因此,我对多个化合物运行了多次计算,每个化合物都可以产生多个结果。我有什么办法可以澄清这一点吗?

So I am having a problem with a Guava TreeBasedTable (if you're unfamiliar, it's a tree that accesses its elements based on a pair of keys) which over the past week has been a bear to figure out. I'll do my best to explain, removing superfluous code:

TreeBasedTable<RowValue, Data, Result> results = TreeBasedTable.create();    
for (Data d : data.getData()) {
            for (Operation o: data.getOperations()) {
                Result r = o.calculate(...);      
                    results.put(r.rowValue, d, r);
    }
}

Basically, I iterate over some data that I have, do some calculations, and stick the results in the table. What's strange though, is when I try to access the elements. If I simply iterate over them as follows:

for(Result r : results.values()){
    System.out.println(r);
}

everything works normally. However, if I instead try to access them as follows:

for(RowValue row : results.rowKeySet()){
    for(Data d : results.columnKeySet()){
        System.out.println(results.get(row, d));
    }
}

The first element is null somehow. If however the tree is of size 1, it works fine. Could it be there is something about Trees going on here that I am not understanding? Sorry for the long question, I hope it was clear.

::EDIT:: The first value passed into the tree is always non-null. When the tree reaches size 3 however, it turns from non-null, to null. Sorry if it wasn't exactly clear what my problem was.
As requested, here is the actual code with the actual keys:

public void createResults(Options options, MeasuredData data, ArrayList methods) {

private TreeBasedTable<MethodDescriptor, Compound, Result> results = TreeBasedTable.create();

for (Compound c : data.getCompounds()) {
    for (Method method : methods) {
        ArrayList<Result> calcResults = method.calculate(c, options);
           for (Result r : calcResults) {                   
               results.put(r.getMethod(), c, r);
           }
     }
}

So I run a number of computations on a number of compounds, each of which can produce multiple results. Is there any way I can clarify this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森末i 2024-12-07 14:55:00

rowKeySet() 是表中所有行的集合,columnKeySet() 是表中所有列的集合。然而,行键和列键的每个组合很可能不会有一个值。例如,您可能只为其中一个 Data 对象计算了具有特定 RowValue 的结果。在这种情况下,该 RowValue 对象(行键)和任何其他 Data 对象(列键)的组合不会映射到 Result。您可能会看到类似的情况。

要仅迭代有效映射,您需要执行以下操作:

for (RowValue row : results.rowKeySet()) {
  // Only iterate over the columns that actually have values for this row
  for (Data d : results.row(row).keySet()) {
    System.out.println(results.get(row, d));
  }
}

编辑:

根据我对所发生情况的理解,没有将 null 设置为桌子。相反,类似的事情正在发生:

Table<Integer, Integer, String> table = TreeBasedTable.create();
table.put(1, 1, "Foo");
table.put(2, 2, "Bar");

请注意,在整个表中,有行 12 以及列 12。但是,仅在 1 行的 1 列和 2 行的 2 列有一个值。因此,当我们迭代行和列的每种可能的组合时,就像您在示例中所做的那样,我们打印出行 1、列 2 和行 的值>2,列 1,两者都返回 null,因为这些单元格中没有放置任何值...调用 table.contains(1, 2) 返回false

The rowKeySet() is the set of all rows and the columnKeySet() is the set of all columns for the table. However, it may well not be the case that there is a value for every combination of row key and column key. For example, maybe you only calculated a result with a certain RowValue for one of the Data objects. The combination of that RowValue object (row key) and any other Data object (column key) would not map to a Result in that case. It seems likely that you're seeing something like this.

To only iterate over the valid mappings, you'd want to do something like this:

for (RowValue row : results.rowKeySet()) {
  // Only iterate over the columns that actually have values for this row
  for (Data d : results.row(row).keySet()) {
    System.out.println(results.get(row, d));
  }
}

Edit:

From my understanding of what's happening, no null is being set as a value in the table. Rather, something like this is happening:

Table<Integer, Integer, String> table = TreeBasedTable.create();
table.put(1, 1, "Foo");
table.put(2, 2, "Bar");

Note that in the entire table, there are rows 1 and 2 and columns 1 and 2. However, there is only a value at column 1 of row 1 and at column 2 of row 2. So when we iterate through every possible combination of row and column, as you do in your example, we print out the values at row 1, column 2 and at row 2, column 1, both of which return null since no value was placed at those cells... calling table.contains(1, 2) returns false.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文