API question for point lambdafication

Brian Goetz brian.goetz at oracle.com
Tue Feb 19 12:50:45 PST 2013

The general advice is "be lenient in what you accept, and specific in 
what you return."  Which would argue for returning Stream<String>. 
Unless creating that String is very expensive and avoidable (neither of 
which are necessarily the case.)  So I think you're probably better off 
with Stream<String> since that's what people want.

BTW, the map parameter could be Object::toString instead of c -> 

On 2/19/2013 3:45 PM, Ben Evans wrote:
> Hi,
> I've got my regex point lambdafication patch going against the current
> state of lambda, but now I have an API question I'd like some feedback
> on.
> (Btw, if this is more appropriate for core-libs just let me know, and
> I'll take my carcass over there & bug those guys instead.)
> I currently have this new method on java.util.regex.Pattern:
> public Stream<CharSequence> splitAsStream(final CharSequence input);
> This provides a stream of values from the input CharSequence, split
> around occurrences of this pattern.
> However, as the return type of splitAsStream() is
> Stream<CharSequence>, then we need to map stream values back to String
> to be most useful, like this:
> 	List<String> out = p.splitAsStream(s)
>                              .map(cs -> cs.toString())
>                              .collect(Collectors.toList());
> So, my question is this - should I continue to use the above
> signature, or should it be:
> public Stream<String> splitAsStream(final CharSequence input);
> This avoids the need for the intermediate map(), which seems like a
> bit of a wart to me.
> Pattern has a vanilla split() method, which returns String[] - so for
> those 2 reasons I'm minded towards the second form.
> Anyone else have any thoughts about this?
> Ben

More information about the lambda-dev mailing list