树中的第一个元素变为空?(First element in tree becoming null? Guava TreebasedTable)

编程入门 行业动态 更新时间:2024-10-25 12:29:19
树中的第一个元素变为空?(First element in tree becoming null? Guava TreebasedTable)

所以我遇到了一个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)); } }

第一个元素以某种方式为null。 但是,如果树的大小为1,则可以正常工作。 难道这里有一些关于树木的东西,我不理解? 很抱歉这个问题很长,我希望很清楚。

:: EDIT ::传递给树的第一个值始终为非null。 当树到达大小3时,它从非null变为null。 对不起,如果不清楚我的问题是什么。 根据要求,这是实际密码的实际代码:

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 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?

最满意答案

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

请注意,在整个表中,有第1行和第2行以及第2列和第2列。 但是,第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.

更多推荐

本文发布于:2023-07-06 07:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047562.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第一个   为空   元素   element   TreebasedTable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!