Guava Table Example

0

Guava Table is a collection that represents a table like structure containing rows, columns and the associated cell value.

The row and column act as an ordered pair of keys. If we have to manage this the traditional way then the structure would be a map of rows where each row contain a map of column and the cell values, for example, Map<rowKey, Map<colKey, cellValue>>.

In this example, we will store software versions and training details in a table.

Since Table is supported by two maps, the primary being the row map and the secondary being column map, any lookups by row key are always faster than lookups by column key. If the row key is available then the method call will run quickly, for example,

table.column("Java").get("Language"));

If don’t provide the row key then any method call with just the column key will have to iterate through all the rows. For example,

column(columnKey).size();

Guava provides different variety of implementations for Table interface.

  1. ArrayTable – backed by a two-dimensional array
  2. TreeBasedTable – backed by a TreeMap
  3. HashBasedTable – backed by a HashMap
  4. ImmutableTable – backed by an ImmutableMap

Our example will use hash based implementation, create().

HashBasedTable.create()

There are many convenience methods in Table interface to retrieve data in different form of views. For example:

  1. rowMap() returns the table data in form of map as a Map<R, Map<C, V>>
  2. rowKeySet() returns set of row keys.
  3. row(rowKey) returns a Map<C, V> of column values.
  4. Likewise you have similar methods for columns: columnMap(), columnKeySet(), and column(c).
  5. cellSet() returns a view of the Table as a set of Table.Cell<R,C, V>.

Retrieve the row, col and cell value using cell entries. Each Cell object has access to its row key, column key and the value.

for (Cell<String, String, String> cell : table.cellSet()) {
     System.out.println("(" + cell.getRowKey() + "," + cell.getColumnKey() + ")=" + cell.getValue());
}

GuavaTableExample:

package com.javarticles.guava;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import com.google.common.collect.Table.Cell;


public class GuavaTableExample {
    
    public static void main(String[] args) {
        Table<String, String, String> table = HashBasedTable.create();
        table.put("Language", "Java", "1.8");
        table.put("Language", "Ruby", "2.2.3");
        table.put("IOC", "Spring", "4.1.4");
        table.put("IOC", "Guice", "4.0");
        table.put("Database", "Oracle", "12c");
        table.put("Database", "MySQL", "5.0");
        table.put("Training", "Java", "Java 8 Features");
        table.put("Training", "Spring", "Spring Core");
        
        System.out.println(table);
        
        System.out.println("Languages: " + table.row("Language"));
        System.out.println("Java language ver# using row: " + table.row("Language").get("Java"));
        System.out.println("Table as row map: " + table.rowMap());
        System.out.println("Row keys as set: " + table.rowKeySet());
        
        System.out.println("Java Column size: " + table.column("Java").size());
        System.out.println("Java: " + table.column("Java"));
        System.out.println("Java language ver# using column: " + table.column("Java").get("Language"));
        System.out.println("Table as col map: " + table.columnMap());
        System.out.println("Column keys as set: " + table.columnKeySet());
        
        System.out.println("Cell set: " + table.cellSet());
        for (Cell<String, String, String> cell : table.cellSet()) {
            System.out.println("(" + cell.getRowKey() + "," + cell.getColumnKey() + ")=" + cell.getValue());
        }
    }   
}

Output:

{Training={Spring=Spring Core, Java=Java 8 Features}, Language={Java=1.8, Ruby=2.2.3}, Database={Oracle=12c, MySQL=5.0}, IOC={Spring=4.1.4, Guice=4.0}}
Languages: {Java=1.8, Ruby=2.2.3}
Java language ver# using row: 1.8
Table as row map: {Training={Spring=Spring Core, Java=Java 8 Features}, Language={Java=1.8, Ruby=2.2.3}, Database={Oracle=12c, MySQL=5.0}, IOC={Spring=4.1.4, Guice=4.0}}
Row keys as set: [Training, Language, Database, IOC]
Java Column size: 2
Java: {Training=Java 8 Features, Language=1.8}
Java language ver# using column: 1.8
Table as col map: {Spring={Training=Spring Core, IOC=4.1.4}, Java={Training=Java 8 Features, Language=1.8}, Ruby={Language=2.2.3}, Oracle={Database=12c}, MySQL={Database=5.0}, Guice={IOC=4.0}}
Column keys as set: [Spring, Java, Ruby, Oracle, MySQL, Guice]
Cell set: [(Training,Spring)=Spring Core, (Training,Java)=Java 8 Features, (Language,Java)=1.8, (Language,Ruby)=2.2.3, (Database,Oracle)=12c, (Database,MySQL)=5.0, (IOC,Spring)=4.1.4, (IOC,Guice)=4.0]
(Training,Spring)=Spring Core
(Training,Java)=Java 8 Features
(Language,Java)=1.8
(Language,Ruby)=2.2.3
(Database,Oracle)=12c
(Database,MySQL)=5.0
(IOC,Spring)=4.1.4
(IOC,Guice)=4.0

Download the source code

This was an example about Guava Table.

You can download the source code here: guavaTableExamples.zip
Share.

Comments are closed.