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


Groups > comp.lang.java.programmer > #33355

Re: streams list in list reduce

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: streams list in list reduce
Date 2015-12-16 09:23 -0500
Organization A noiseless patient Spider
Message-ID <n4rs00$rj7$1@dont-email.me> (permalink)
References <3360df16-6ac2-4ee4-b6b4-2fcaac9e6aa7@googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web