Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #33354 > unrolled thread

streams list in list reduce

Started byPhilipp Kraus <philipp.kraus.flashpixx@gmail.com>
First post2015-12-16 02:52 -0800
Last post2015-12-16 23:41 -0800
Articles 4 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  streams list in list reduce Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2015-12-16 02:52 -0800
    Re: streams list in list reduce Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-12-16 09:23 -0500
    Re: streams list in list reduce Arne Vajhøj <arne@vajhoej.dk> - 2015-12-16 21:14 -0500
      Re: streams list in list reduce Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2015-12-16 23:41 -0800

#33354 — streams list in list reduce

FromPhilipp Kraus <philipp.kraus.flashpixx@gmail.com>
Date2015-12-16 02:52 -0800
Subjectstreams list in list reduce
Message-ID<3360df16-6ac2-4ee4-b6b4-2fcaac9e6aa7@googlegroups.com>
Hello,

I have got a definition like

interface Execute
{
    Collection<Term> exec();
}

and run vai streams over my data

List<Execute> x; // x is filled

And I would like to create 
List<Term> y = x.stream().map( i -> i.exec()).collect( ?? toList() );

I would like to run addAll on the collect and build a concatinated list if all lists which are return by the exec method. How can I create this concatinated list?

Thanks

Phil

[toc] | [next] | [standalone]


#33355

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2015-12-16 09:23 -0500
Message-ID<n4rs00$rj7$1@dont-email.me>
In reply to#33354
On 12/16/2015 5:52 AM, Philipp Kraus wrote:
> Hello,
>
> I have got a definition like
>
> interface Execute
> {
>      Collection<Term> exec();
> }
>
> and run vai streams over my data
>
> List<Execute> x; // x is filled
>
> And I would like to create
> List<Term> y = x.stream().map( i -> i.exec()).collect( ?? toList() );
>
> I would like to run addAll on the collect and build a concatinated list if all lists which are return by the exec method. How can I create this concatinated list?

     Untested (not even thoroughly thought out):

	List<Term> y = x.stream().map(i -> i.exec())
	    .collect(
	        () -> new ArrayList<>(),
	        (c,e) -> c.addAll(e),
	        (c1,c2) -> c1.addAll(c2)
	     );

Those intermediate ArrayList's look silly, but I don't see a way to
avoid them.  Perhaps a different construct could do so, but ...

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

[toc] | [prev] | [next] | [standalone]


#33356

FromArne Vajhøj <arne@vajhoej.dk>
Date2015-12-16 21:14 -0500
Message-ID<n4t5pf$96b$1@speranza.aioe.org>
In reply to#33354
On 12/16/2015 5:52 AM, Philipp Kraus wrote:
> I have got a definition like
>
> interface Execute { Collection<Term> exec(); }
>
> and run vai streams over my data
>
> List<Execute> x; // x is filled
>
> And I would like to create List<Term> y = x.stream().map( i ->
> i.exec()).collect( ?? toList() );
>
> I would like to run addAll on the collect and build a concatinated
> list if all lists which are return by the exec method. How can I
> create this concatinated list?

Demo of one approach:

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class LotsOfLists {
	public static interface Execute
	{
	     public Collection<Integer> exec();
	}
	public static class ExecuteImpl implements Execute {
		private List<Integer> data = new ArrayList<>();
		public ExecuteImpl(int... vals) {
			for(int val : vals) data.add(val);
		}
		@Override
		public Collection<Integer> exec() {
			return data;
		}
	}
	public static void main(String[] args) {
		List<Execute> x = new ArrayList<>();
		x.add(new ExecuteImpl(1));
		x.add(new ExecuteImpl(2, 3));
		List<Integer> res = x.stream().collect(ArrayList<Integer>::new, 
(acc,elm) -> acc.addAll(elm.exec()), ArrayList<Integer>::addAll);
		System.out.println(res);
	}

}

Arne

[toc] | [prev] | [next] | [standalone]


#33359

FromPhilipp Kraus <philipp.kraus.flashpixx@gmail.com>
Date2015-12-16 23:41 -0800
Message-ID<a88e73da-2931-46fe-af64-adb4057f99f8@googlegroups.com>
In reply to#33356
I have found a solution myself, I use .flatMap( i -> i.exec() ).collect( Collectors::toList() )

Thanks for help

Phil


Am Donnerstag, 17. Dezember 2015 03:14:31 UTC+1 schrieb Arne Vajhøj:
> On 12/16/2015 5:52 AM, Philipp Kraus wrote:
> > I have got a definition like
> >
> > interface Execute { Collection<Term> exec(); }
> >
> > and run vai streams over my data
> >
> > List<Execute> x; // x is filled
> >
> > And I would like to create List<Term> y = x.stream().map( i ->
> > i.exec()).collect( ?? toList() );
> >
> > I would like to run addAll on the collect and build a concatinated
> > list if all lists which are return by the exec method. How can I
> > create this concatinated list?
> 
> Demo of one approach:
> 
> import java.util.ArrayList;
> import java.util.Collection;
> import java.util.List;
> 
> public class LotsOfLists {
> 	public static interface Execute
> 	{
> 	     public Collection<Integer> exec();
> 	}
> 	public static class ExecuteImpl implements Execute {
> 		private List<Integer> data = new ArrayList<>();
> 		public ExecuteImpl(int... vals) {
> 			for(int val : vals) data.add(val);
> 		}
> 		@Override
> 		public Collection<Integer> exec() {
> 			return data;
> 		}
> 	}
> 	public static void main(String[] args) {
> 		List<Execute> x = new ArrayList<>();
> 		x.add(new ExecuteImpl(1));
> 		x.add(new ExecuteImpl(2, 3));
> 		List<Integer> res = x.stream().collect(ArrayList<Integer>::new, 
> (acc,elm) -> acc.addAll(elm.exec()), ArrayList<Integer>::addAll);
> 		System.out.println(res);
> 	}
> 
> }
> 
> Arne

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web