1

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.

Grzegorz Rożniecki
  • 27,415
  • 11
  • 90
  • 112
sidss
  • 923
  • 1
  • 12
  • 20
  • 3
    The `IllegalAccessError` looks like a compiler bug. Try to replace the method references by equivalent lambda expressions. I vaguely remember having seen a similar issue here. – Holger Aug 23 '16 at 10:08
  • What do you want to do with duplicates? – shmosel Aug 23 '16 at 20:10
  • For duplicates, i just want to override it with latest entry, which HashBasedTable does. – sidss Aug 23 '16 at 21:17
  • Replaced them with Lambda expression and it worked. Thanks. – sidss Aug 24 '16 at 00:50
  • 2
    Possible duplicate of [Java 8 collector for Guava Immutable Table](http://stackoverflow.com/questions/38985659/java-8-collector-for-guava-immutable-table) – Grzegorz Rożniecki Aug 24 '16 at 07:25

1 Answers1

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))
                    );
sidss
  • 923
  • 1
  • 12
  • 20