Hot questions for Operating Table in Guava

Top 10 Java Open Source / Guava / Operating Table

Print all keys and value for HashBasedTable in Google Guava

Question: I create and populate a Guava Table using the following code:

Table<String, String, Integer> table = HashBasedTable.create();
table.put("A", "B", 1);
table.put("A", "C", 2);
table.put("B", "D", 3);

I wonder how to iterate over table and print out both keys and value for each row? So, the desired output is:

A B 1
A C 2
B D 3

Answer: Im not Guava user so this may be overkill (if it is true then will be glad for any info) but you can use table.rowMap() to get Map<String, Map<String, Integer>> which will represents data in table in form {A={B=1, C=2}, B={D=3}}. Then just iterate over this map like:

Map<String, Map<String, Integer>> map = table.rowMap();

for (String row : map.keySet()) {
    Map<String, Integer> tmp = map.get(row);
    for (Map.Entry<String, Integer> pair : tmp.entrySet()) {
        System.out.println(row+" "+pair.getKey()+" "+pair.getValue());


for (Map.Entry<String, Map<String,Integer>> outer : map.entrySet()) {
    for (Map.Entry<String, Integer> inner : outer.getValue().entrySet()) {
        System.out.println(outer.getKey()+" "+inner.getKey()+" "+inner.getValue());

or even better using

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

Guava table to CSV

Question: I am attempting to export a Guava table to CSV. The code below works, but it skips the first column which I want to see in the output as well. Can you suggest anything?

obviously using values() and keySet() separately works.

final RowSortedTable<String, String, Double> graph = TreeBasedTable.create();

graph.put("A", "0", 0.0);
graph.put("A", "1", 1.0);
graph.put("B", "0", 0.1);
graph.put("B", "1", 1.1);

final Appendable out = new StringBuilder();
try {
    final CSVPrinter printer = CSVFormat.DEFAULT.print(out);

                    .map(x -> x.values())//

} catch (final IOException e) {


Answer: You'll want to use entrySet() instead of values() and then map each entry to a list of its key and values:

        .map(entry -> ImmutableList.builder()

Define guava HashBasedTable/Table in spring xml config

Question: I'm trying to create and populate a guava HashBasedTable in spring xml config file but I haven't been able to.

My table looks like this:

Table<String, Foo, Bar> myTable;

And I've tried this in my xml but don't know how put new value into the table:

<property name="myTable">
        <bean class="" factory-method="create">
            <!--- how do I insert value in here??? -->

Answer: If you want to do this exclusively in xml, it's a bit tricky: I see guava doesn't offer too many options for putting values in that Table. There is an approach, but it's weird for more than one insert:

<bean id="myTable" class="" factory-method="create" />

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject">
        <ref local="myTable" />
    <property name="targetMethod">
    <property name="arguments">