Proposal: Replace foreach loop with Iterable.forEach in String.join(CharSequence, Iterable)

Donald Raab donraab at gmail.com
Tue Aug 17 04:55:29 UTC 2021


The following code:

for (CharSequence cs: elements) {
    joiner.add(cs);
}

Can be replaced with:

elements.forEach(joiner::add);

This would have the minor benefit of making it safe to use String.join with synchronized collections without requiring a client side lock. There are likely other opportunities like this in the JDK. 

Also, with the addition of forEach on Iterable, and Predicate in Java 8, the following design FAQ is outdated.

https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/doc-files/coll-designfaq.html#a6 <https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/doc-files/coll-designfaq.html#a6> 

Thanks,
Don


More information about the core-libs-dev mailing list