Convert old "into" code to the collectors
Paul Sandoz
paul.sandoz at oracle.com
Wed Jan 16 07:59:46 PST 2013
You are wiring up the wrong parameters to both toCollection and toList.
Here are the relevant methods:
public static<T, C extends Collection<T>>
Collector<T,C> toCollection(Supplier<C> collectionFactory) {
return leftCombiningReducer(collectionFactory, Collection::add, Collection::addAll);
}
public static<T>
Collector<T,List<T>> toList() {
// @@@ Consider a tree-based List, if we can solve the post-processing problem
return toCollection(ArrayList<T>::new);
}
The former takes a supplier:
Collectors.<...>toCollection(ArrayList<...>::new);
The latter takes no parameters:
Collectors.<...>toList();
Notice how the latter calls the former.
Hth,
Paul.
On Jan 16, 2013, at 4:41 PM, Marcos Antonio <marcos_antonio_ps at hotmail.com> wrote:
>
> Here are the compiler error messages for the two case:
>
> List<PropriedadeChavePai> propriedadesChavePai = new ArrayList<>();
>
> chave.getPropriedadesChavePai().stream().
> map(PropriedadeChavePai::new).
> collect(Collectors.<PropriedadeChavePai>toCollection(propriedadesChavePai));
>
> [javac] C:\desenvolvimento\desktop\desenvolvimento\br\desenvolvimento\dados\CriadorChave.java:259: error: method toCollection in class Collectors cannot be applied to given types;
> [javac] collect(Collectors.<PropriedadeChavePai>toCollection(propriedadesChavePai));
> [javac] ^
> [javac] required: Supplier<C>
> [javac] found: List<PropriedadeChavePai>
> [javac] reason: actual and formal argument lists differ in length
> [javac] where C,T are type-variables:
> [javac] C extends Collection<T> declared in method <T,C>toCollection(Supplier<C>)
> [javac] T extends Object declared in method <T,C>toCollection(Supplier<C>)
> [javac] 1 error
>
> --------------------------------------------------------------------------
>
> List<PropriedadeChavePai> propriedadesChavePai = new ArrayList<>();
>
> chave.getPropriedadesChavePai().stream().
> map(PropriedadeChavePai::new).
> collect(Collectors.<PropriedadeChavePai>toList(propriedadesChavePai));
>
> [javac] C:\desenvolvimento\desktop\desenvolvimento\br\desenvolvimento\dados\CriadorChave.java:259: error: method toList in class Collectors cannot be applied to given types;
> [javac] collect(Collectors.<PropriedadeChavePai>toList(propriedadesChavePai));
> [javac] ^
> [javac] required: no arguments
> [javac] found: List<PropriedadeChavePai>
> [javac] reason: actual and formal argument lists differ in length
> [javac] 1 error
>
> Marcos
>
> ----------------------------------------
>> Date: Wed, 16 Jan 2013 15:05:22 +0000
>> From: maurizio.cimadamore at oracle.com
>> To: marcos_antonio_ps at hotmail.com
>> CC: lambda-dev at openjdk.java.net
>> Subject: Re: Convert old "into" code to the collectors
>>
>> On 16/01/13 14:34, Marcos Antonio wrote:
>>> Hello, everybody!
>>>
>>> Before build b73 I had code like this:
>>>
>>> List<PropriedadeChavePai> propriedades = new ArrayList<>();
>>>
>>> chave.getPropriedadesChavePai().stream().
>>> map(PropriedadeChavePai::new).
>>> into(propriedades);
>>>
>>> Now I'm having trouble trying to convert it to use the new Collectors. I have tried something like this:
>>>
>>> chave.getPropriedadesChavePai().stream().
>>> map(PropriedadeChavePai::new).
>>> collect(Collectors.toCollection(propriedades));
>>>
>>> but it didn't work and I couldn't figure out the right thing to do.
>>>
>>> Thank you in advance.
>>>
>>> Marcos
>>>
>> What do you mean it didn't work? Did you get an error message from the
>> compiler? If so, can you please paste it?
>>
>> Maurizio
>
More information about the lambda-dev
mailing list