Guava Table Example

Guava Table

A special collection that allows us to use two keys as indexes, a row key and a column key with a value. Trying to achieve the same this using Java Collections you you need to have something like <CompanyName, <Map EmployeeName, YearsOfService>> which is ugly at best and a bit on the unwieldy side. However, by using Guava Table HashBasedTable you are able to create and maintain quite easily.

@GwtCompatible
public interface Table<R,C,V>

Guava Table Methods Available

#Method and Description
1Set<Table.Cell<R,C,V>> cellSet()
Returns a set of all row key / column key / value triplets.
2void clear()
Removes all mappings from the table.
3Map<R,V> column(C columnKey)
Returns a view of all mappings that have the given column key.
4Set<C> columnKeySet()
Returns a set of column keys that have one or more values in the table.
5Map<C,Map<R,V>> columnMap()
Returns a view that associates each column key with the corresponding map from row keys to values.
6boolean contains(Object rowKey, Object columnKey)
Returns true if the table contains a mapping with the specified row and column keys.
7boolean containsColumn(Object columnKey)
Returns true if the table contains a mapping with the specified column.
8boolean containsRow(Object rowKey)
Returns true if the table contains a mapping with the specified row key.
9boolean containsValue(Object value)
Returns true if the table contains a mapping with the specified value.
10boolean equals(Object obj)
Compares the specified object with this table for equality.
11V get(Object rowKey, Object columnKey)
Returns the value corresponding to the given row and column keys, or null if no such mapping exists.
12int hashCode()
Returns the hash code for this table.
13boolean isEmpty()
Returns true if the table contains no mappings.
14V put(R rowKey, C columnKey, V value)
Associates the specified value with the specified keys.
15void putAll(Table<? extends R,? extends C,? extends V> table)
Copies all mappings from the specified table to this table.
16V remove(Object rowKey, Object columnKey)
Removes the mapping, if any, associated with the given keys.
17Map<C,V> row(R rowKey)
Returns a view of all mappings that have the given row key.
18Set<R> rowKeySet()
Returns a set of row keys that have one or more values in the table.
19Map<R,Map<C,V>> rowMap()
Returns a view that associates each row key with the corresponding map from column keys to values.
20int size()
Returns the number of row key / column key / value mappings in the table.
21Collection<V> values()
Returns a collection of all values, which may contain duplicates.

Guava Table Implementations

There are four implementation classes available for Table:

  • HashBasedTable
  • TreeBasedTable
  • ImmutableTable
  • ArrayTable

Guava Table Java Example

package com.avaldes;

import java.util.Map;

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

public class GuavaTableExample {
  public static void main(String[] args) {
    Table<String, String, Double> employeeYearsOfService = HashBasedTable.create();
    // Employer is the Row, Employee Name is the Column, # of Years is the Value
    employeeYearsOfService.put("AT&T", "Amaury Valdes", 1.4);
    employeeYearsOfService.put("Microsoft", "Bill Smith", 13.2);
    employeeYearsOfService.put("Google", "Dan Houston", 11.5);
    employeeYearsOfService.put("Microsoft", "Stacy Lerner", 3.5);
    employeeYearsOfService.put("AT&T", "Michelle Bailey", 2.0);
    employeeYearsOfService.put("Google", "Bill Smith", 9.75);
    
    System.out.println("\n---[Find all AT&T Employees]-----");
      Map<String,Double> attEmployees =  employeeYearsOfService.row("AT&T");
      for(Map.Entry<String, Double> employee : attEmployees.entrySet()){
        System.out.println("Employee Name: " + employee.getKey() + ", Years of Service: " + employee.getValue());
      }
      
    System.out.println("\n---[Find all Employees Named 'Bill Smith']-----");
      Map<String,Double> employees =  employeeYearsOfService.column("Bill Smith");
      
      
      for(Map.Entry<String, Double> employee : employees.entrySet()){
        System.out.println("Employee Company: " + employee.getKey() + ", Years of Service: " + employee.getValue());
      }
  }
}

That’s It!

I hope you enjoyed this tutorial. Please continue to share the love and like us so that we can continue bringing you quality tutorials. Happy Coding!!!

GuavaTable

Related Posts

Please Share Us on Social Media

Facebooktwitterredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *