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 |
---|---|
1 | Set<Table.Cell<R,C,V>> cellSet() Returns a set of all row key / column key / value triplets. |
2 | void clear() Removes all mappings from the table. |
3 | Map<R,V> column(C columnKey) Returns a view of all mappings that have the given column key. |
4 | Set<C> columnKeySet() Returns a set of column keys that have one or more values in the table. |
5 | Map<C,Map<R,V>> columnMap() Returns a view that associates each column key with the corresponding map from row keys to values. |
6 | boolean contains(Object rowKey, Object columnKey) Returns true if the table contains a mapping with the specified row and column keys. |
7 | boolean containsColumn(Object columnKey) Returns true if the table contains a mapping with the specified column. |
8 | boolean containsRow(Object rowKey) Returns true if the table contains a mapping with the specified row key. |
9 | boolean containsValue(Object value) Returns true if the table contains a mapping with the specified value. |
10 | boolean equals(Object obj) Compares the specified object with this table for equality. |
11 | V get(Object rowKey, Object columnKey) Returns the value corresponding to the given row and column keys, or null if no such mapping exists. |
12 | int hashCode() Returns the hash code for this table. |
13 | boolean isEmpty() Returns true if the table contains no mappings. |
14 | V put(R rowKey, C columnKey, V value) Associates the specified value with the specified keys. |
15 | void putAll(Table<? extends R,? extends C,? extends V> table) Copies all mappings from the specified table to this table. |
16 | V remove(Object rowKey, Object columnKey) Removes the mapping, if any, associated with the given keys. |
17 | Map<C,V> row(R rowKey) Returns a view of all mappings that have the given row key. |
18 | Set<R> rowKeySet() Returns a set of row keys that have one or more values in the table. |
19 | Map<R,Map<C,V>> rowMap() Returns a view that associates each row key with the corresponding map from column keys to values. |
20 | int size() Returns the number of row key / column key / value mappings in the table. |
21 | Collection<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!!!
Related Posts
- Guava Multimap - ArrayListMultimap Implementation Example
In this tutorial we will cover implementing an ArrayListMultimap (which allows duplicate key/value pairs). - Guava Multimap - HashMultimap Implementation Example
In this tutorial we will cover implementing an HashMultimap which does not allow duplicate key/value pairs, using HashMultimap. - Guava Multimap - TreeMultimap Implementation Example
In this tutorial we will cover a implementing an TreeMultimap which performs Sorting according to natural order and does not allow duplicate key/value pairs. - Guava Multiset Example
In this tutorial we will cover a basic introduction the to Guava's Multiset collection using easy to follow example to show you how best to implement this collection in your own code. - Guava BiMap Example (Bi-Directional Map)
In this tutorial we will cover a basic introduction the to Guava's Bidirectional Map (BiMap) using easy to follow example to show you how best to implement this collection in your own code. - Guava Table Example
In this tutorial we will cover a basic introduction the to Guava's Table using easy to follow example to show you how best to implement this collection in your own code. - Guava ClassToInstanceMap Example
In this tutorial we show you how to use Guava's ClassToInstanceMap collection to store class type as the key and the instance of the collection as a value.
Leave a Reply