SUNDONG
SUNDONG

Reputation: 2769

Any library supports Guava tables to csv format?

Are there any library supports Guava tables to csv format? I generated

RowSortedTable<String, String, Double> graph

But the generating process takes some time(some query processing is needed), so I want to save this intermediate result and want to read and use it again.

Upvotes: 2

Views: 3691

Answers (2)

user987541
user987541

Reputation: 56

You can use Apache Commons CSV:

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);
// 0.0,1.0
// 0.1,1.1

Upvotes: 4

To create a CSV file using Guava you can use Joiner.

  Joiner.on(",").join(yourStrings)

Try to drop the output of this utility into a file whith extension CSV.

Upvotes: -3

Related Questions