通过返回ImmutableTable<Integer,String,Boolean> process(String item) { /*...*/}
的方法的字符串处理列表。例如ImmutableTable
。
收集结果,即合并所有结果(单个表可能包含重复项)并返回 final ImmutableTable<Integer, String, Boolean> result =
itemsToProcess.parallelStream()
.map(item ->
ProcessorInstanceProvider.get()
.buildTable(item))
.collect(toImmutableTable());
public static <R, C, V> Collector<ImmutableTable<R, C, V>,
ImmutableTable.Builder<R, C, V>, ImmutableTable<R, C, V>>
toImmutableTable() {
return Collector.of(
ImmutableTable.Builder<R, C, V>::new,
ImmutableTable.Builder<R, C, V>::putAll,
(
a,
b) -> a.putAll(b.build()),
ImmutableTable.Builder::build);
}
。
当没有重复时,我当前的实现工作:
ImmutableTable
但收集HashBaseTable
时失败,因为存在重复的行列条目,因此构建失败。
如何防止构建失败?如何使用T
,它将与重复项一起使用。类似于ImmutableTable
- A
,HashBasedTable
- R
和ImmutableTable
- final HashBasedTable<Integer, String, Boolean> 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
,因为-.default
会使用最新的条目覆盖现有条目,如果我们尝试重复输入,则不会失败,并返回聚合的不可变表。
答案 0 :(得分:1)
用Lambda表达式替换方法引用并且它有效。
ImmutableTable.copyOf(itemList.parallelStream()
.map(item ->
ProcessorInstanceProvider.get()
.build(item))
.collect(() -> HashBasedTable.create(),
(a, b) -> a.putAll(b),
(a, b) -> a.putAll(b))
);