树中的第一个元素变为空?番石榴树基表
所以我遇到了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
rowKeySet()
是表中所有行的集合,columnKeySet()
是表中所有列的集合。然而,行键和列键的每个组合很可能不会有一个值。例如,您可能只为其中一个Data
对象计算了具有特定RowValue
的结果。在这种情况下,该RowValue
对象(行键)和任何其他Data
对象(列键)的组合不会映射到Result
。您可能会看到类似的情况。要仅迭代有效映射,您需要执行以下操作:
编辑:
根据我对所发生情况的理解,没有将
null
设置为桌子。相反,类似的事情正在发生:请注意,在整个表中,有行
1
和2
以及列1
和2
。但是,仅在1
行的1
列和2
行的2
列有一个值。因此,当我们迭代行和列的每种可能的组合时,就像您在示例中所做的那样,我们打印出行1
、列2
和行的值>2
,列1
,两者都返回null
,因为这些单元格中没有放置任何值...调用table.contains(1, 2)
返回false
。The
rowKeySet()
is the set of all rows and thecolumnKeySet()
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 certainRowValue
for one of theData
objects. The combination of thatRowValue
object (row key) and any otherData
object (column key) would not map to aResult
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:
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:Note that in the entire table, there are rows
1
and2
and columns1
and2
. However, there is only a value at column1
of row1
and at column2
of row2
. So when we iterate through every possible combination of row and column, as you do in your example, we print out the values at row1
, column2
and at row2
, column1
, both of which returnnull
since no value was placed at those cells... callingtable.contains(1, 2)
returnsfalse
.