Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: Can I put a filter into a lambda? Date: Mon, 4 Dec 2017 09:29:11 -0500 Organization: A noiseless patient Spider Lines: 46 Message-ID: References: <321ee28f-bbea-4565-a48e-c0476d9a78d4@googlegroups.com> <9545f24b-39da-4eab-8c65-365a299b3b2a@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Mon, 4 Dec 2017 14:29:14 -0000 (UTC) Injection-Info: reader02.eternal-september.org; posting-host="e61e15c5e7400baf9d7d8a7d2cc0d6e3"; logging-data="18988"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18tXJvQ8xELj2aWKM/xXD0i" User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.5.0 In-Reply-To: <9545f24b-39da-4eab-8c65-365a299b3b2a@googlegroups.com> Content-Language: en-US Cancel-Lock: sha1:rZYvbrFUo7PtVJxlfgmip1MF/eo= Xref: csiph.com comp.lang.java.programmer:37716 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.