Java:合并两个com.google.common.collect.Table

Java:合并两个com.google.common.collect.Table,java,java-8,guava,guava-table,Java,Java 8,Guava,Guava Table,我有两张桌子,像: 在表2中 2L Fruits 10 2L Vegetables 40 3L Fruits 15 4L Vegetables 35 因此,总数为: 1L Fruits 20 2L Fruits 30 + 10 = 40 2L Vegetables 15 + 40 = 55 3L Vegetables 10 3L Fruits 15 4L Vegetables 35 我希望使用

我有两张桌子,像:

在表2中

   2L Fruits     10
   2L Vegetables 40
   3L Fruits     15
   4L Vegetables 35
因此,总数为:

   1L Fruits     20
   2L Fruits     30 + 10 = 40
   2L Vegetables 15 + 40 = 55
   3L Vegetables 10
   3L Fruits     15
   4L Vegetables 35
我希望使用Java8流式解决方案,但也可以使用经典的解决方案。

您可以使用:

您可以使用:


到目前为止你试过什么?到目前为止你试过什么?a,b->a+b。。。整数::suma,b->a+b。。。整数::和
   1L Fruits     20
   2L Fruits     30
   2L Vegetables 15
   3L Vegetables 10
   2L Fruits     10
   2L Vegetables 40
   3L Fruits     15
   4L Vegetables 35
   1L Fruits     20
   2L Fruits     30 + 10 = 40
   2L Vegetables 15 + 40 = 55
   3L Vegetables 10
   3L Fruits     15
   4L Vegetables 35
Table<Long, String, Integer> tableOne = HashBasedTable.create();
tableOne.put(1L, "Fruits", 20);
tableOne.put(2L, "Fruits", 30);
tableOne.put(2L, "Vegetables", 15);
tableOne.put(3L, "Vegetables", 10);

Table<Long, String, Integer> tableTwo = HashBasedTable.create();
tableTwo.put(2L, "Fruits", 10);
tableTwo.put(2L, "Vegetables", 40);
tableTwo.put(3L, "Fruits", 15);
tableTwo.put(4L, "Vegetables", 35);


HashBasedTable<Long, String, Integer> sumTable = Stream.concat(tableOne.cellSet().stream(), tableTwo.cellSet().stream())
        .collect(Tables.toTable(Table.Cell::getRowKey,
                Table.Cell::getColumnKey,
                Table.Cell::getValue,
                Integer::sum, HashBasedTable::create));

sumTable.cellSet().forEach(System.out::println);
(1,Fruits)=20
(2,Fruits)=40
(2,Vegetables)=55
(3,Vegetables)=10
(3,Fruits)=15
(4,Vegetables)=35