Design for collections upgrades

Sam Pullara sam at sampullara.com
Tue Mar 8 13:32:13 PST 2011


I think this is exactly the issue we are talking about. It is unclear what the performance of the list after it is returned to me. I'd be much happier a one liner that looked like:

List<T> filtered = new ArrayList(otherList.filter(predicate));

or

List<T> filtered = otherList.filter(predicate).to(new ArrayList<>());

Generally you shouldn't be even looking at the result. What would be much better is that you just perform the next action:

otherList.filter(predicate).each(operation);

or

for (T t : otherList.filter(predicate)) {
 operation
}

Sam

On Mar 8, 2011, at 1:24 PM, Brian Goetz wrote:

>>> Further, I think the choice of eager/lazy is a reasonable one to give
>>> the programmer.  Programmers who do not need the features offered by
>>> laziness shouldn't need to learn how laziness works just to use the new
>>> collection operations.
>> 
>> I don't see the need of having a eager filter().
> 
> Java programs have lots of code that basically looks like this:
> 
> ArrayList<T> filtered = new ArrayList<T>();
> for (T t : otherList)
>     if (predicate(t))
>         filtered.add(t);
> 
> This can be replaced by the one liner:
> 
> ArrayList<T> filtered = otherList.filter(predicate);
> 
> Not only is it shorter, it is clearer, less error-prone, and does not 
> force the programmer to use side effects to do the computation.
> 
> 



More information about the lambda-dev mailing list