Two questions about forEach

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Wed Aug 8 04:47:51 PDT 2012


On 08/08/12 11:51, Jose wrote:
> 1. This code won't compile because of the return type of add(). Is this
> behaviour on porpouse?
>
> public static void main(String[] args) {
>          List<String> names=new ArrayList<>();
>          Set<String> registry=new HashSet<>();
>          names.forEach(n->registry.add(n));
> }
Hi Jose
try with:

         names.forEach(n-> { registry.add(n); });

we are exploring some enhancements in this area.
>
> This forces to me to use map() instead, and get an iterable of booleans.
>
>
> 2. Why forEach() returns void?. In this way the pipeline is sealed at the
> end, no more pieces can be attached to it.
>    It would better returning the incomming iterable (its elements maybe
> modified by the lambda)
>
>
I'll leave this to our library gurus...

Maurizio


More information about the lambda-dev mailing list