Java 8 使用 HashBasedTable 作为累加器的 Guava ImmutableTable 收集器给出 IllegalAccessError
Java 8 collector for Guava ImmutableTable using HashBasedTable as accumulator gives IllegalAccessError
通过 returns ImmutableTable<R,C,V>
方法处理字符串列表。例如ImmutableTable<Integer,String,Boolean> process(String item) { /*...*/}
.
收集结果,即合并所有结果(单个 table 可能包含重复项)和 return 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
会用最新条目覆盖现有条目,并且如果我们尝试放置重复条目也不会失败,和 return 聚合 immutable table.
用 Lambda 表达式替换了方法引用并且有效。
ImmutableTable.copyOf(itemList.parallelStream()
.map(item ->
ProcessorInstanceProvider.get()
.build(item))
.collect(() -> HashBasedTable.create(),
(a, b) -> a.putAll(b),
(a, b) -> a.putAll(b))
);
通过 returns ImmutableTable<R,C,V>
方法处理字符串列表。例如ImmutableTable<Integer,String,Boolean> process(String item) { /*...*/}
.
收集结果,即合并所有结果(单个 table 可能包含重复项)和 return 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
会用最新条目覆盖现有条目,并且如果我们尝试放置重复条目也不会失败,和 return 聚合 immutable table.
用 Lambda 表达式替换了方法引用并且有效。
ImmutableTable.copyOf(itemList.parallelStream()
.map(item ->
ProcessorInstanceProvider.get()
.build(item))
.collect(() -> HashBasedTable.create(),
(a, b) -> a.putAll(b),
(a, b) -> a.putAll(b))
);