使用HashBasedTable作为累加器的Guava ImmutableTable的Java 8收集器给出了IllegalAccessError

通过返回ImmutableTable方法的字符串处理列表。 例如ImmutableTable process(String item) { /*...*/}

收集结果,即合并所有结果(单个表可能包含重复项)并返回ImmutableTable

当没有重复项时,我当前的实现工作:

  final ImmutableTable result = itemsToProcess.parallelStream() .map(item -> ProcessorInstanceProvider.get() .buildTable(item)) .collect(toImmutableTable()); public static  Collector<ImmutableTable, ImmutableTable.Builder, ImmutableTable> toImmutableTable() { return Collector.of( ImmutableTable.Builder::new, ImmutableTable.Builder::putAll, ( a, b) -> a.putAll(b.build()), ImmutableTable.Builder::build); } 

但收集ImmutableTable时失败,因为存在重复的行列条目,因此构建失败。

如何防止构建失败? 我如何使用HashBaseTable ,它将与重复项一起使用。 像TImmutableTableAHashBasedTableRImmutableTable内存使用量最少吗?

试过:

  final HashBasedTable result = listOfItems.parallelStream() .map(item -> ProcessorInstanceProvider.get() .build(item) ) .collect( Collector.of( HashBasedTable::create, HashBasedTable::putAll, (a, b) -> { a.putAll(b); return a; })); 

但是获得运行时错误:

 Caused by: java.lang.IllegalAccessError: tried to access class com.google.common.collect.AbstractTable 

对于HashTable::putAll

我们如何使用HashBasedTable作为累加器来收集ImmutablesTable ,因为HashBasedTable用最新的条目覆盖现有条目,如果我们尝试放入重复条目并返回聚合的不可变表,则不会失败。

用Lambda表达式替换了方法引用,它起作用了。

 ImmutableTable.copyOf(itemList.parallelStream() .map(item -> ProcessorInstanceProvider.get() .build(item)) .collect(() -> HashBasedTable.create(), (a, b) -> a.putAll(b), (a, b) -> a.putAll(b)) );