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


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

Re: Can I put a filter into a lambda?

Started byEric Sosman <esosman@comcast-dot-net.invalid> (Eric Sosman)
First post2017-12-04 09:29 +1200
Last post2017-12-08 15:00 +1200
Articles 3 — 3 participants

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

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Can I put a filter into a lambda? Eric Sosman <esosman@comcast-dot-net.invalid> (Eric Sosman) - 2017-12-04 09:29 +1200
    Re: Can I put a filter into a lambda? Eric Douglas <e.d.programmer@gmail.com> (Eric Douglas) - 2017-12-04 07:14 +1200
    Re: Can I put a filter into a lambda? Sebastian <nowhere@nirvana.org> (Sebastian) - 2017-12-08 15:00 +1200

#37751 — Re: Can I put a filter into a lambda?

FromEric Sosman <esosman@comcast-dot-net.invalid> (Eric Sosman)
Date2017-12-04 09:29 +1200
SubjectRe: Can I put a filter into a lambda?
Message-ID<4063469279@f38.n261.z1.binkp.net>
On 12/4/2017 8:32 AM, Eric Douglas wrote:
> On Monday, December 4, 2017 at 8:20:59 AM UTC-5, Eric Sosman wrote:
>>       If it *must* be an ArrayList (and not some other kind of List),
>> you might want to change the last bit to
>>
>>      .collect(Collectors.toCollection(ArrayList::new));
>>
>> Although the toList() method appears to use ArrayList anyhow, it
>> doesn't guarantee to do so -- meaning that it might use some other
>> List in a future Java, or in circumstances I haven't studies (e.g.,
>> it might behave differently with a parallel stream).
>>
> ok, thanks, I'll keep that in mind.  I'll have to test if the variable can be
assigned and if that would be more efficient.  Currently it doesn't matter what
 kind of list because I made the rowToModelIndex variable final, just calling
clear() then addAll().

     If you'd rather clear and repopulate the same List (as opposed
to making a new one each time), you could use forEach():

        rowModelIndex.clear();
        IntStream.range(0, modelRowCount)
             .filter((value) -> include(value))
            .forEach((value) -> rowModelIndex.add(value);

or

        rowModelIndex.clear();
        IntStream.range(0, modelRowCount)
            .forEach((value) ->
                if (include(value)) rowModelIndex.add(value));

I don't know which of these might be faster, or whether either might be faster
than a plain old `for' loop.  (Research project.)

     FWIW, Brian Goetz (who knows far more about JVM performance
issues than I ever will) indicates that object allocation and collection is
cheaper than commonly thought, so there's little if any advantage to re-using
the same List object:

https://www.ibm.com/developerworks/java/library/j-jtp09275/index.html

... which means even more research may be indicated.  (The cited article is
twelve years old, and we may expect that JVM's have continued to improve
meanwhile).

--
esosman@comcast-dot-net.invalid
Eleven hundred forty-three days to go.

[toc] | [next] | [standalone]


#37752

FromEric Douglas <e.d.programmer@gmail.com> (Eric Douglas)
Date2017-12-04 07:14 +1200
Message-ID<2977346321@f38.n261.z1.binkp.net>
In reply to#37751
On Monday, December 4, 2017 at 9:29:33 AM UTC-5, Eric Sosman wrote:
>      If you'd rather clear and repopulate the same List (as opposed
> to making a new one each time), you could use forEach():
>
>       rowModelIndex.clear();
>       IntStream.range(0, modelRowCount)
>              .filter((value) -> include(value))
>           .forEach((value) -> rowModelIndex.add(value);
>
> or
>
>       rowModelIndex.clear();
>       IntStream.range(0, modelRowCount)
>           .forEach((value) ->
>               if (include(value)) rowModelIndex.add(value));
>
> I don't know which of these might be faster, or whether either
> might be faster than a plain old `for' loop.  (Research project.)
>

Thanks!  I'll check it out.

>      FWIW, Brian Goetz (who knows far more about JVM performance
> issues than I ever will) indicates that object allocation and
> collection is cheaper than commonly thought, so there's little
> if any advantage to re-using the same List object:
>
> https://www.ibm.com/developerworks/java/library/j-jtp09275/index.html
>
> ... which means even more research may be indicated.  (The cited
> article is twelve years old, and we may expect that JVM's have
> continued to improve meanwhile).
>

I'd have to take another look at their code for clear() and addAll(), I would
guess generating a new variable might be cheaper, but the functional difference
 is more important.  I'd have to check if I have something else grabbing a
reference to this variable just expecting it to get updated values rather than
needing to get the reference to a new variable.  I believe I did that, though
offhand can't recall why.

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


#37771

FromSebastian <nowhere@nirvana.org> (Sebastian)
Date2017-12-08 15:00 +1200
Message-ID<1913807097@f38.n261.z1.binkp.net>
In reply to#37751
Am 04.12.2017 um 15:29 schrieb Eric Sosman:

>
> I don't know which of these might be faster, or whether either
> might be faster than a plain old `for' loop.  (Research project.)
>
>     FWIW, Brian Goetz (who knows far more about JVM performance
> issues than I ever will) indicates that object allocation and
> collection is cheaper than commonly thought, so there's little
> if any advantage to re-using the same List object:
>
> https://www.ibm.com/developerworks/java/library/j-jtp09275/index.html
>
> ... which means even more research may be indicated.  (The cited
> article is twelve years old, and we may expect that JVM's have
> continued to improve meanwhile).
>

Angelika Langer did some research into the performance of streams vs. for loops
 when JDK 8 came out. It's referenced from this article, which puts
performance, readability and maintainability into perspective:

https://blog.jooq.org/2015/12/08/3-reasons-why-you-shouldnt-replace-your-for-lo
ops-by-stream-foreach/

-- Sebastian

[toc] | [prev] | [standalone]


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


csiph-web