Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #33857
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: stream filter twice |
| Date | 2016-02-20 22:20 -0500 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <nabaej$1rfq$1@gioia.aioe.org> (permalink) |
| References | <04334e10-3bad-49dc-87d8-dc2f00f90807@googlegroups.com> |
On 2/20/2016 7:28 AM, Philipp Kraus wrote:
> 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?
What is your goal? To produce easily understandable code? To save
a few micro seconds?
If the first then I think you current code is fine.
If the second then you will need to be creative.
Here is my attempt to be creative:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class MultiProcessing {
public static <T> List<T> original(List<T> lst, Predicate<? super T>
p1, Predicate<? super T> p2) {
List<T> x = lst.stream().filter(p1).collect(Collectors.toList());
if(!x.isEmpty()) return x;
List<T> y = lst.stream().filter(p2).collect(Collectors.toList());
if(!y.isEmpty()) return y;
return lst;
}
public static <T> List<T> alternative(List<T> lst, Predicate<? super T>
p1, Predicate<? super T> p2) {
List<T> x = new ArrayList<>();
List<T> y = new ArrayList<>();
lst.stream().forEach(v -> { if(p1.test(v)) x.add(v); if(x.isEmpty() &&
p2.test(v)) y.add(v); });
if(!x.isEmpty()) return x;
if(!y.isEmpty()) return y;
return lst;
}
private static <T> void test(List<T> lst, Predicate<? super T> p1,
Predicate<? super T> p2) {
System.out.println(original(lst, p1, p2));
System.out.println(alternative(lst, p1, p2));
}
public static void main(String[] args) {
List<Integer> lst = Arrays.asList(1, 4, 9, 16);
test(lst, v -> v > 2, v -> v < 2);
test(lst, v -> v > 20, v -> v < 2);
test(lst, v -> v > 20, v -> v < -2);
}
}
Arne
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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