Search code examples
javaguavatreetable

Guava TreeRow of TreeBasedTable not sorted despite it implements SortedMap?


Is this by design ?

Map<String, Map<String, String>> rowMap = treeTable.rowMap();
Map<String, String> notSortedTreeRow = rowMap.get(rowId);

so that you can have rowMap sorted (as SortedMap) but you can't have sorted notSortedTreeRow (TreeRow) which is [column, value] ?

EDIT: It was my fault, I had a String representation of Integers and I was on the impression that numerical Strings were compared based on the numerical value :-)

Something like this if I oversimplify it:

TreeBasedTable<String, String, String> table = TreeBasedTable.create();

table.put("1", "8", "x");
table.put("1", "9", "x");
table.put("1", "10", "x");

Map<String,String> map = table.rowMap().get("1");

Solution

  • Just do:

    SortedMap<String, String> sortedRow = (SortedMap<String, String>) rowMap.get(rowId);
    

    as it is TreeRow object instance, which is SortedMap. It is guarateed by RowSortedTable interface, and it does not override Table's rowMap argument, because it is not possible in Java.

    EDIT:

    Answering original question here:

    Is this by design?

    Yes, it's by Java's design.