Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #33858
| Path | csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail |
|---|---|
| From | Robert Klemme <shortcutter@googlemail.com> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: stream filter twice |
| Date | Sun, 21 Feb 2016 12:58:18 +0100 |
| Lines | 65 |
| Message-ID | <ditn2rF45nnU1@mid.individual.net> (permalink) |
| References | <04334e10-3bad-49dc-87d8-dc2f00f90807@googlegroups.com> <nabaej$1rfq$1@gioia.aioe.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8; format=flowed |
| Content-Transfer-Encoding | quoted-printable |
| X-Trace | individual.net XS7Kaz/qTDpWDBlyw+0lSwx+03NExUkQdpRz5E6u7KK+Z3eLo= |
| Cancel-Lock | sha1:+HXT4Aj/s6KiyolWkKn6omiPpX8= |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 |
| In-Reply-To | <nabaej$1rfq$1@gioia.aioe.org> |
| Xref | csiph.com comp.lang.java.programmer:33858 |
Show key headers only | View raw
On 21.02.2016 04:20, Arne Vajhøj wrote:
> What is your goal? To produce easily understandable code? To save
> a few micro seconds?
That is really the first question to answer.
> 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:
I'm a bit more old fashioned, maybe that's why I do not find the
advantage of streams so big here. I was also considering, we might not
know how large the initial collection is. So the cost of iterating
twice could be significant. So:
public static List<myClass> test_2(final Collection<myClass>
myclasscollection) {
final List<myClass> x = new ArrayList<>();
final List<myClass> y = new ArrayList<>();
for (final myClass i : myclasscollection) {
if (i.filter())
x.add(i);
else if (i.anotherfilter()) // "else" is optimization!
y.add(i);
}
if (!x.isEmpty()) {
return x;
} else if (!y.isEmpty()) {
return y;
} else
return new ArrayList<>(myclasscollection);
}
public static List<myClass> test_3(final Collection<myClass>
myclasscollection) {
final List<myClass> x = new ArrayList<>();
final List<myClass> y = new ArrayList<>();
for (final myClass i : myclasscollection) {
if (i.filter())
x.add(i);
else if (i.anotherfilter()) // "else" is optimization!
y.add(i);
}
return x.isEmpty() ?
(y.isEmpty() ? new ArrayList<>(myclasscollection) : y) :
x;
}
Note: formatting suffered a bit.
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
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