The Table interface introduced in Guava is helpful in implementing Tabular data, such as data to be written to a CSV. Think about it as a spreadsheet. All the data in a spreadsheet can be represented by 3 parameters: the row number, column number, and the actual value stored in the cell. Hence the Table<R, C, V> interface has 3 generic parameters too.
/*
| Name | GPA
0 | Bob | 2.3
1 | Jim | 3.4
2 | Tim | 2.8
*/
Table<Integer, String, Object> studentData = TreeBasedTable.create();
studentData.put(0, "Name", "Bob");
studentData.put(1, "Name", "Jim");
studentData.put(2, "Name", "Tim");
studentData.put(0, "GPA", "2.3");
studentData.put(1, "GPA", "3.4");
studentData.put(2, "GPA", "2.8");Important instance methods:
Map<R, V> column(C columnKey): Returns a map of Row→Value for the given column. For e.g.,studentData.column("Name")in the above case would return aMapthat looks like:{ 0: "Bob", 1: "Jim", 2: "Time"}.Map<C, V> row(R rowKey): Returns a map of Column→Value for the given row. For e.g.,studentData.row(2)would return aMapthat looks like:{ "Name" : "Tim", "GPA" : 2.8 }
Implementations:
All the implementations of Table can be used by the static Table<R,C,V> create() method, except for ImmutableTable. As the name suggests, this implementation builds an immutable object. Hence we need to build it using the provided Builder, i.e., ImmutableTable.Builder<R,C,V>. Calling the build() instance method of the Builder will return an immutable Table.
