番石榴表到CSV

Luc*_*ryn 3 java csv guava

我正在尝试将Guava表导出为CSV.下面的代码有效,但它跳过了我想在输出中看到的第一列.你能提出什么建议吗?

编辑:显然使用values()keySet()单独工作.

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);
Run Code Online (Sandbox Code Playgroud)

编辑:这也不起作用:

        printer.printRecords(//
                graph.rowMap().entrySet().stream().map(entry -> {
                      List a = Arrays.asList(entry.getKey());
                      a.addAll(entry.getValue().values());
                      return a;
                    }).collect(Collectors.toList())
                );
Run Code Online (Sandbox Code Playgroud)

mfu*_*n26 6

您将要使用entrySet()而不是values()将每个条目映射到其键和值的列表:

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