Convert old "into" code to the collectors

Marcos Antonio marcos_antonio_ps at hotmail.com
Wed Jan 16 08:25:28 PST 2013


So the bottom line is that I cannot use an existing collection anymore as I used to do with the "into" method, right?
Are there any alternatives?

For the time being, this is my workaround:

propriedadesChavePai.addAll(
    chave.getPropriedadesChavePai().stream().
        map(PropriedadeChavePai::new).
        collect(Collectors.<PropriedadeChavePai>toList())
);

Very bulky.

Marcos

----------------------------------------
> Date: Wed, 16 Jan 2013 15:58:29 +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
>
>
> I think the toCollection method is designed to convert a constructor
> reference (passed as a Supplier) into a new Collector, which is used to
> carry out the 'collect' operation. In other words, I don't think you can
> pass a plain collection to 'toCollection'.
>
> Maurizio
>
>
> On 16/01/13 15:41, Marcos Antonio 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