Two questions about forEach

Jose jgetino at telefonica.net
Wed Aug 8 03:51:47 PDT 2012


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));
}

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)



More information about the lambda-dev mailing list