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());
    }
}

or

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 com.google.common.collect.Table.Cell

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);

    printer.printRecords(//
            graph.rowMap().values()//
                    .stream()//
                    .map(x -> x.values())//
                    .collect(Collectors.toList()));

} catch (final IOException e) {
    e.printStackTrace();
}

System.out.println(out);

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

printer.printRecords(graph.rowMap().entrySet()
        .stream()
        .map(entry -> ImmutableList.builder()
                .add(entry.getKey())
                .addAll(entry.getValue().values())
                .build())
        .collect(Collectors.toList()));

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="com.google.common.collect.HashBasedTable" factory-method="create">
            <!--- how do I insert value in here??? -->
        </bean>
</property>

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="com.google.common.collect.HashBasedTable" factory-method="create" />

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