使用HashBasedTable作为累加器的Guava ImmutableTable的Java 8收集器给出了IllegalAccessError [英] Java 8 collector for Guava ImmutableTable using HashBasedTable as accumulator gives IllegalAccessError

查看:319
本文介绍了使用HashBasedTable作为累加器的Guava ImmutableTable的Java 8收集器给出了IllegalAccessError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

处理字符串的方法,该方法返回 ImmutableTable< R,C,V> 。例如 ImmutableTable< Integer,String,Boolean> process(String item){/*...*/ }

Process list of string via method which returns ImmutableTable<R,C,V>. For instance ImmutableTable<Integer,String,Boolean> process(String item) { /*...*/}.

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

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);
  }

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

But it fails while collecting ImmutableTable as there are duplicate row-column entries and hence build fails.

如何防止构建失败?我如何使用 HashBaseTable 哪个可以使用重复项。类似 T - ImmutableTable A - HashBasedTable R - ImmutableTable 内存使用量最少?

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?

尝试:

 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 用最新的条目覆盖现有条目,如果我们尝试放入重复条目并返回聚合的不可变表,则不会失败。

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.

推荐答案

用Lambda表达式替换方法引用并且它有效。

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))
                    );

这篇关于使用HashBasedTable作为累加器的Guava ImmutableTable的Java 8收集器给出了IllegalAccessError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆