番石榴表到CSV [英] Guava table to CSV

查看:83
本文介绍了番石榴表到CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

显然,分别使用values()keySet()是有效的.

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

这也不起作用:

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

推荐答案

您将要使用 values() ,然后将每个条目映射到其键和值的列表:

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

这篇关于番石榴表到CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆