Equivalent of Maps.uniqueIndex?

Michael Nascimento misterm at gmail.com
Fri Jan 4 07:59:21 PST 2013


Ok, here we go.

I can only use build 69. In this build, Tabulator.mappedTo type
parameters seem to be the other way around.

Assume I have a bunch of entities with a getId() : Long. What I want is:

// with Guava and JDK 8
Map<Long, Entity> entityById = Maps.uniqueIndex(entities, e -> e.getId());

With pure JDK 8, here is my best attempt:

Map<Long, Entity> entityById =
entities.parallelStream().tabulate(Tabulators.<Long,Entity>mappedTo(e
-> e.getId(), (i1, i2) -> i1));

which doesn't work. This:

entities.parallelStream().tabulate(Tabulators.<Entity,Long>mappedTo(e
-> e.getId(), (i1, i2) -> i1));

compiles, but is pretty useless.

Suggestions?

Regards,
Michael


On Fri, Jan 4, 2013 at 1:41 PM, Brian Goetz <brian.goetz at oracle.com> wrote:
> Stuff is in flux, apologies.
>
> In the current tip, Tabulators became Accumulators, and we have these
> versions which take a T->U, plus optionally a Supplier<Map> and a merge
> function (the default throws on duplicates.)
>
>     public static <T, U> Accumulator<T, Map<T,U>> mappedTo(Function<? super
> T, ? extends U> mapper) {
>         return mappedTo(mapper, (BinaryOperator<U>) THROWING_MERGER);
>     }
>
>     public static <T, U> Accumulator<T, Map<T,U>> mappedTo(Function<? super
> T, ? extends U> mapper,
>
> BinaryOperator<U> mergeFunction) {
>         return mappedTo(mapper, mergeFunction, (Supplier<Map<T,U>>)
> HashMap::new);
>     }
>
>     public static <T, U, M extends Map<T, U>> Accumulator<T, M>
> mappedTo(Function<? super T, ? extends U> mapper,
>
>   Supplier<M> mapSupplier) {
>         return mappedTo(mapper, (BinaryOperator<U>) THROWING_MERGER,
> mapSupplier);
>     }
>     public static <T, U, M extends Map<T, U>> Accumulator<T, M>
> mappedTo(Function<? super T, ? extends U> mapper,
>
>   BinaryOperator<U> mergeFunction,
>
>   Supplier<M> mapSupplier) {
>         return new MergingMapAccumulator<T, T, U, M>(mapSupplier,
> mergeFunction) {
>             @Override
>             public void accumulate(M map, T value) {
>                 accumulate(map, value, mapper.apply(value));
>             }
>         };
>
>     }
>
>
> On 1/4/2013 10:37 AM, Michael Nascimento wrote:
>>
>> There is no mapTo() in Tabulators in build 69. There is a mappedTo,
>> but it takes a BinaryOperator. Which one am I supposed to use?
>>
>> Regards,
>> Michael
>>
>> On Fri, Jan 4, 2013 at 1:28 PM, Brian Goetz <brian.goetz at oracle.com>
>> wrote:
>>>
>>> tabulate(Tabulators.mapTo())
>>>
>>>
>>> On 1/4/2013 10:22 AM, Michael Nascimento wrote:
>>>>
>>>>
>>>> Guys,
>>>>
>>>> What is the equivalent in Lambda to Guava's Maps.uniqueIndex:
>>>>
>>>>
>>>>
>>>> http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/Maps.html#uniqueIndex(java.lang.Iterable,
>>>> com.google.common.base.Function)
>>>>
>>>> I was trying the tabulate method, but couldn't find a method in
>>>> Tabulators with a single function that assumes the stream has unique
>>>> keys.
>>>>
>>>> Regards,
>>>> Michael
>>>>
>>>
>


More information about the lambda-dev mailing list