Process list of string via method which returns ImmutableTable<R,C,V>
. For instance ImmutableTable<Integer,String,Boolean> process(String item) { /*...*/}
.
Collect the result i.e, merge all results (individual table may contain duplicates) and return ImmutableTable
.
My current implementation works when there are no duplicates:
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);
}
But it fails while collecting ImmutableTable
as there are duplicate row-column entries and hence build fails.
How can i prevent build failure ? How can i use HashBaseTable
which will work with duplicates. Something like T
- ImmutableTable
, A
- HashBasedTable
and R
- ImmutableTable
with minimum memory usage?
Tried with:
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;
}));
But getting runtime error:
Caused by: java.lang.IllegalAccessError: tried to access class com.google.common.collect.AbstractTable
for HashTable::putAll
.
How can we use HashBasedTable
as accumulator to collect ImmutablesTable
, as HashBasedTable
overrides the existing entry with latest one and doesn't fail if we try to put duplicate entry, and return aggregated immutable table.