3

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?

3
  • That wasn't quite what I meant by a small, self-contained example. I meant more like something using Strings or Integer keys that I can actually run to see the behavior you're describing. That said, would Result.getMethod() return the same Method or different ones for each Result produced by a call to method.calculate? If the same, you'd need to store some kind of Collection<Result> value for each Compound/Method combination or you're going to overwrite results.
    – ColinD
    Aug 23, 2011 at 15:18
  • Yeah, sorry about that. I am suspecting at the moment that there is something going on in my compareTo method for the MethodDescriptor (kind of like what you just suggested), so I am working through that. If that solves it I'll let you know. Thanks so much!
    – Steve
    Aug 23, 2011 at 15:47
  • Yep, the comparator was screwed up! Thank you so much for your help!
    – Steve
    Aug 23, 2011 at 16:01

1 Answer 1

5

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.

2
  • Okay so you are correct in that it no longer prints out the null value (upvote). However, my problem is more than the value is null in the first place. What happens is, the first value is passed in (non-null), the second value is passed in, and then when the third is passed in, the first value turns to null.
    – Steve
    Aug 23, 2011 at 13:47
  • @Steve: So you're saying that you pass in 2 non-null values and then when you pass in a 3rd, you only see a null and the 2nd and 3rd values in the table? If you see all the values you expect when iterating through values() or iterating in the way I described, I still think what I described is what's happening. A short, self-contained example including actual keys and values would help.
    – ColinD
    Aug 23, 2011 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.