This post will discuss about Guava Table in Java.

You may have encountered situations where you need to store and manipulate data in a two-dimensional structure, such as a spreadsheet or a matrix. For example, you may want to keep track of the grades of students in different courses, or the sales of products in different regions. How would you implement such a data structure in Java?

 
One option is to use a nested map, such as Map<String, Map<String, Integer>>, where the first key is the row, the second key is the column, and the value is the cell. However, this approach has some drawbacks:

  • It is verbose and cumbersome to write and read.
  • It requires checking for null values and creating inner maps when inserting new data.
  • It does not provide any convenient methods to access or manipulate the data by rows, columns, or cells.

Another option is to use an array or a list of arrays or lists, such as int[][] or List<List<Integer>>, where the indices represent the rows and columns, and the elements are the cells. However, this approach also has some drawbacks:

  • It requires knowing the size of the data structure beforehand and resizing it when necessary.
  • It does not allow using arbitrary objects as row or column keys, only integers.
  • It does not provide any convenient methods to access or change the data by rows, columns, or cells.

Fortunately, there is a better option: using the Guava Table.

1. Overview of Guava Table?

The Guava Table is a collection that represents a table-like structure containing rows, columns, and the associated cell values. The row and the column act as an ordered pair of keys that can be any objects, not just integers. The cell value can also be any object. The Guava Table provides many benefits over the nested map or the array/list approaches:

  • It is concise and elegant to write and read.
  • It handles null values and creates inner collections automatically when inserting new data.
  • It provides many convenient methods to access or manipulate the data by rows, columns, or cells.
  • It can handle sparse data, where only a few cells have values, without wasting memory or performance.
  • It can support multiple types of row keys and column keys, such as strings, integers, enums, etc.
  • It can provide views of the data by rows, columns, or cells, and perform operations such as filtering, transforming, or sorting on them.
  • It can create custom table implementations based on different criteria, such as hash-based, tree-based, array-based, or immutable.

The Guava Table is part of the Google Guava library, which is a set of core Java libraries that provide useful utilities and enhancements for common tasks. You can add the Guava dependency to your project using Maven or Gradle.

2. How to Create a Guava Table?

To use Guava Table in your Java project, you need to add the Guava dependency to your build file, such as Maven or Gradle. Then you can create an instance of Guava Table in multiple ways using one of the factory methods from the classes HashBasedTable, TreeBasedTable, ArrayTable, or ImmutableTable. These are discussed below:

1. The create() method from the class HashBasedTable uses LinkedHashMap internally and creates a table that preserves the insertion order of the rows and columns.

 
2. The create() method from the class TreeBasedTable uses TreeMap internally and creates a table that sorts the rows and columns by their natural ordering or by supplying comparators.

 
3. The create() method from the class ArrayTable uses a two-dimensional array internally and creates a table that has a fixed size and requires knowing the row and column keys beforehand.

Download Code

 
4. The builder() method from the class ImmutableTable follows a builder pattern and creates an immutable table that does not allow any modifications after creation.

Download Code

3. How to Use a Guava Table?

Once you have created a Guava Table instance, you can use its methods to store and retrieve data in various ways. Some of the common methods of the Guava Table interface are:

  • put(R rowKey, C columnKey, V value): Inserts or updates the value for the given row key and column key in the table.
  • get(R rowKey, C columnKey): Returns the value for the given row key and column key in the table, or null if none exists.
  • remove(R rowKey, C columnKey): Removes the value for the given row key and column key from the table, if any, and returns it.
  • contains(R rowKey, C columnKey): Returns true if the table contains a value for the given row key and column key.
  • rowKeySet(): Returns a view of all the row keys.
  • columnKeySet(): Returns a view of all the column keys.
  • values(): Returns a view of all the cell values.
  • cellSet(): Returns a set of all cells in the table, each represented by a Table.Cell object that contains the row key, column key, and value.
  • row(R rowKey): Returns a view of the table as a map from column keys to values for the given row key.
  • column(C columnKey): Returns a view of the table as a map from row keys to values for the given column key.
  • rowMap(): Returns a view of the entire table as a nested map from row keys to column keys to values.

Here is a sample code demonstrating the usage of these methods:

Download Code

4. Conclusion

The Guava Table is a powerful and convenient collection that allows you to store and manipulate data in a two-dimensional structure. It offers many advantages over the traditional nested map or array/list approaches and provides many methods to access or modify the data by rows, columns, or cells. If you are looking for a simple and elegant way to handle tabular data in Java, you should definitely give the Guava Table a try.

If you want to learn more about Guava Table, you can check out the Guava official website or its GitHub repository.