本文介绍了Guava不可变表的Java 8收集器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用例:

处理字符串的方法,该方法返回类型为 {R,C,V}的 ImmutableTable 。例如 ImmutableTable of {Integer,String,Boolean} process(String item){...}

收集结果ie,合并所有结果并返回 ImmutableTable 。有没有办法实现它?

Collect the result i.e, merge all results and return ImmutableTable. Is there a way to achieve it?

当前实施(如波希米亚人所建议):

Current implementation (as suggested by Bohemian):

如何使用并行流?以下代码中是否存在并发问题?使用并行流我在tableBuilder.build()上得到索引1800处的NullPointerException,但在流中工作正常。

How about using parallel stream ? Is there any concurrency issues in the below code? With Parallel stream i an getting "NullPointerException at index 1800" on tableBuilder.build(), but works fine with stream.

ImmutableTable<Integer, String, Boolean> buildData() {   

  // list of 4 AwsS3KeyName   
listToProcess.parallelStream() 

  //Create new instance via Guice dependency injection 
.map(s3KeyName -> ProcessorInstanceProvider.get()    
.fetchAndBuild(s3KeyName)) 
.forEach(tableBuilder::putAll); 

 return tableBuilder.build(); }

虽然下面的代码与流和并行流有关。但是由于row和col的重复输入,ImmutableBuild失败了。什么是在合并表时防止重复的最佳方法?

While below code worksgreat with stream as well as parallel stream. But ImmutableBuild is failing due to duplicate entry for row and col. What could be the best way to prevent duplicates while merging tables ?

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::new, 
ImmutableTable.Builder::putAll, (builder1, builder2) -> 
builder1.putAll(builder2.build()), ImmutableTable.Builder::build); }

编辑:
如果在合并不同的表时ImmutableTable.Builder中有任何重复的条目然后它失败了,

Edit : If there is any duplicate entry in ImmutableTable.Builder while merging different tables then it fails,

试图通过将ImmutableTables放入HashBasedTable来避免faluire

Trying to avoid faluire by putting ImmutableTables in HashBasedTable

  ImmutableTable.copyOf(itemListToProcess.parallelStream()
            .map(itemString ->
           ProcessorInstanceProvider.get()
                    .buildImmutableTable(itemString))
                    .collect(
                            Collector.of(
                                    HashBasedTable::create,
                                    HashBasedTable::putAll,
                                    (a, b) -> {
                                        a.putAll(b);
                                        return a;
                                    }));
  )

但我是获取运行时异常引起:java.lang.I llegalAccessError:尝试访问com.google.common.collect.AbstractTable类。

But i am getting runtime exception "Caused by: java.lang.IllegalAccessError: tried to access class com.google.common.collect.AbstractTable".

我们如何使用HashBasedTable作为累加器来收集ImmutablesTables,因为HashBasedTable用最新的条目覆盖现有条目,如果我们尝试放入重复条目并且不返回,则不会失败聚合不可变表。

How can we use HashBasedTable as Accumulator to collect ImmutablesTables, 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.

推荐答案

这应该有效:

List<String> list; // given a list of String

ImmutableTable result = list.parallelStream()
    .map(processor::process) // converts String to ImmutableTable
    .collect(ImmutableTable.Builder::new, ImmutableTable.Builder::putAll,
        (a, b) -> a.putAll(b.build())
    .build();

这种减少是线程安全的。

This reduction is threadsafe.

或使用 HashBasedTable 作为中间数据结构:

Or using HashBasedTable as the intermediate data structure:

ImmutableTable result = ImmutableTable.copyOf(list.parallelStream()
    .map(processor::process) // converts String to ImmutableTable
    .collect(HashBasedTable::create, HashBasedTable::putAll, HashBasedTable::putAll));

这篇关于Guava不可变表的Java 8收集器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 01:47