2016-07-22 47 views
3

我正试图将番石榴表导出为CSV。下面的代码可以工作,但它跳过了我想在输出中看到的第一列。你能提出什么建议吗?番石榴表格CSV 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); 

编辑:这不起作用或者:

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

你调试代码,检查该列是否仍然存在在传递给打印机的列表中还是不? – Thomas

+0

'CSVPrinter'和'CSVPrinter'从哪里来? –

+0

您的代码在第一行打印“0.0,1.0”,第二行打印为“0.1,1.1”,您的输出是什么? – Xaerxess

回答

6

你要使用entrySet()代替values()然后映射每个条目,其重点和值的列表:

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

这就是它,谢谢! – LucasSeveryn

0
graph.rowMap().values() 

只返回表的值不是键(第一列)这是这样,你没有看到它在CSV。

+0

我知道,因此我的问题如何返回两个。 – LucasSeveryn

0

你只映射到值,你跳过键(他们是第一列)。 你可能会想如果你有更多的字段,你可以随时与地图切换TRIPPLE与

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

检查进一步信息

https://google.github.io/guava/releases/17.0/api/docs/com/google/common/collect/RowSortedTable.html

http://docs.oracle.com/javase/7/docs/api/java/util/SortedMap.html?is-external=true

+0

谢谢,这看起来很有希望。你可以稍微扩大你的答案? II将你的代码放到'printer.printRecords(...)'方法中,它只返回'java.util.stream.ReferencePipeline $ 3 @ 1808f31' – LucasSeveryn

+0

不要忘记以.collect(Collectors.toList())结尾) ; 我没有添加,为简洁起见,但我会更新我的答案是完整的 –

+0

这样做会返回一个异常恐怕...添加了一个编辑。 – LucasSeveryn

2

的文档工作。这与JSon类似,其中每个对象都被描述为一个映射,其中键是属性和值的值。

class Tripple { 
     String value1; 
     String value2; 
     Double value3; 
    Tripple(String value1, String value2, Double value3) { 
     super(); 
     this.value1 = value1; 
     this.value2 = value2; 
     this.value3 = value3; 
    } 

    Map<String,Map<String,Integer>> map; 
RowSortedTable<String, String, Double> maprows; 
List<Tripple> triples = maprows.rowMap().entrySet().stream() 
                .map(t->{final List<Tripple> tripples = new ArrayList<>(); 
                  t.getValue().entrySet() 
                        .forEach(p->{tripples.add(new Tripple(t.getKey(),p.getKey(),p.getValue()));}); 
                  return tripples;}) 
                .flatMap(t->t.stream()) 
                .collect(Collectors.toList()); 
+0

如果有150列而不是3? – LucasSeveryn

+0

@LucasSeveryn这不是什么大事他总是可以把它们放在地图上。地图总是可以替换一个对象。 –

+0

@LucasSeveryn让我澄清。每个对象都可以像Json一样被描述为MAP。其中键是属性名称,值是属性值。如果您的ccolumns数量未定,您可以做的是创建一个包含该布局的地图。 –