LucasSeveryn
LucasSeveryn

Reputation: 6272

Guava table to 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?

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

EDIT: this doesn't work either:

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

Upvotes: 5

Views: 2417

Answers (4)

Silvan Van Leeuwen
Silvan Van Leeuwen

Reputation: 146

You're only mapping to the values, you're skipping the keys (and they are the first column). You'll probably want to work with

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

Check the documentation for further info

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

Upvotes: 0

mfulton26
mfulton26

Reputation: 31264

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

Upvotes: 7

Alexander Petrov
Alexander Petrov

Reputation: 9492

If you have more fields you can always switch the Tripple with a Map. This is similar to JSon where each object is described a a map with keys being the attributes and values the values.

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

Upvotes: 1

Michal Maťovč&#237;k
Michal Maťovč&#237;k

Reputation: 918

graph.rowMap().values()

returns only values of the table not the keys (first column) that's way you don't see it in CSV.

Upvotes: 0

Related Questions