Design for collections upgrades

Gernot Neppert mcnepp02 at googlemail.com
Tue Mar 8 12:56:13 PST 2011


  I was thinking about the need for List comprehension methods to return 
a new Collection.
As others have pointed out, there's always the time and space overhead 
involved, plus the question which Collection type to choose.
And all that simply because Collection.size() exists and has to run with 
reasonable complexity!

Maybe an appropriate return type for list comprehension methods would be 
java.lang.Iterable.

Some advantages:

- Allows creation of lightweight intermediate objects
- Well-known interface
- Immediately usable with foreach
- Could be seamlessly integrated into existing Collections framework.

If all the Standard Collection types get fitted with an additional 
constructor taking an Iterable, one could flexibly choose the target for 
'filter' invocations on a per-case basis:

Set<Thing> fooAbles = new HashSet<>(things-filter( #{ t -> t.isFoo() });

List<Thing> fooAbles = new ArrayList<>(things-filter( #{ t -> t.isFoo() });

And, of course, directly iterate over the result:

for(Thing foo : things-filter( #{ t -> t.isFoo() })) {
...
}


A disadvantage would be the lack of chainable list 
comprehensions...unless, of course, we'd add the planned extension 
methods to java.lang.Iterable,
which is not an altogether absurd idea, I guess.







More information about the lambda-dev mailing list