Search code examples
javahashtableguava

Can Guava Hashtable have columns with different data types?


I need create a Guava Hashtable and store integer and string values in different columns. Can this be done with guava Hashtable ?

Thanks!


Solution

  • Take a look in Table documentation (HashBasedTable is only an implementation of Table - "...which is essentially backed by a HashMap<R, HashMap<C, V>>...")

    A single Table<R, C, V> object represents a single mapping between a row key R, a column key C and a value V and as so, you can have only columns from a single type on a particular Table object.

    To mixture columns you can create two table objects with shares the same keys: Table tableStringCol = HashBasedTable.create(); table.put(1, "doc_name", "Bill"); Table tableIntegerCol = HashBasedTable.create(); table.put(1, "doc_count", 10); table.put(1, "doc_size", 15);

    You can also use a single Table object which have only String columns and then when fetching the values, make a conversion from the relevant columns into Integer. You can read more about Guava's Table here