任何库都支持将 Guava 表转换为 csv 格式吗?

Any library supports Guava tables to csv format?

是否有任何库支持将 Guava 表转换为 csv 格式? 我生成了

RowSortedTable<String, String, Double> graph

但是生成过程需要一些时间(需要一些查询处理),所以我想把这个中间结果保存下来,想再次阅读和使用。

要使用 Guava 创建 CSV 文件,您可以使用 Joiner

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

尝试将此实用程序的输出放入扩展名为 CSV 的文件中。

您可以使用 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