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


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

stream filter twice

Newsgroups comp.lang.java.programmer
Date 2016-02-20 04:28 -0800
Message-ID <04334e10-3bad-49dc-87d8-dc2f00f90807@googlegroups.com> (permalink)
Subject stream filter twice
From Philipp Kraus <philipp.kraus.flashpixx@gmail.com>

Show all headers | View raw


Hello,

I'm using streams to filter some data. I just do this with


List<myClass> x = myclasscollection.stream().filter( i -> i.filter() ).collect( Collectors.toList() );
if (!x.isempty())
   return x;


List<myClass> y = myclasscollection.stream().filter( i -> i.anotherfilter() ).collect( Collectors.toList() );
if (!y.isEmpty())
   return y;

return  myclasscollection.stream().collect( Collectors.toList() );

So my question is, can optimize this with a stream operation. I need to check each object of the collection, but only if no object exist, I need to run the check with another filter.
A change to filter( i -> i.filter() || i.anotherfilter() ) or filter( i-> i.filter() ^ i.anotherfilter() ) is not possible, because I need the empty check of all results.

In a best-case idea I need only one run with the stream and can build the result list in one shoot. Did you have got any idea to optimize this code?

Thanks a lot

Phil

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


Thread

stream filter twice Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2016-02-20 04:28 -0800
  Re: stream filter twice Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-02-20 08:16 -0500
  Re: stream filter twice Arne Vajhøj <arne@vajhoej.dk> - 2016-02-20 22:20 -0500
    Re: stream filter twice Robert Klemme <shortcutter@googlemail.com> - 2016-02-21 12:58 +0100
      Re: stream filter twice Arne Vajhøj <arne@vajhoej.dk> - 2016-02-21 09:52 -0500
      Re: stream filter twice Sebastian <news@seyweiler.dyndns.org> - 2016-02-22 00:13 +0100

csiph-web