Java 8 collector for Guava ImmutableTable using HashBasedTable as accumulator gives IllegalAccessError

隐身守侯 提交于 2020-02-23 05:57:07

问题


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.


回答1:


Replaced method references with Lambda expression and it worked.

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


来源:https://stackoverflow.com/questions/39095205/java-8-collector-for-guava-immutabletable-using-hashbasedtable-as-accumulator-gi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!