Table like java data structure

There is a generic TreeBasedTable class from Google library which does exactly what you are asking for. It also offers many other useful utility methods and its usage is shown in the user guide.

From the TreeBasedTable docs:

Implementation of Table whose row keys and column keys are ordered by their natural ordering or by supplied comparators.

Example usage:

RowSortedTable<Vertex, Vertex, Double> weightedGraph = TreeBasedTable.create();
weightedGraph.put(v2, v3, 4.0);
weightedGraph.put(v1, v2, 20.0);

System.out.println( weightedGraph.rowKeySet() ); // prints [v1, v2]

Leave a Comment