If we can't have closures, can we at least have comprehensions?

Published: 9:11 AM GMT+12, Thursday, 6 November 2008 under: technology

Dear Sun,

If we can't have closures in Java 7, can we at least have comprehensions added to the collections API? It seems I'm forever writing, or pulling in Commons Collections (which lacks generics) or Functional Java to add simple functional list comprehension support to my projects, if the core Java collections API included something as simple as:

public static interface Collector {
  boolean collect(T t);
}

public static  List collect(List list, Collector map) {
  List mappedList = new ArrayList();
  for (T t : list) {
    if (map.collect(t)) {
      mappedList.add(t);
    }
  }
  return mappedList;
}

and several others, then we'd be able to introduce a new standard programming model to Java developers, and practically show them how and why adding closures would benefit them.

Of course, we just just promote the hell out of Functional Java and be done with it :)

Comments (4)

And there was I assuming you meant Python style List Comprehensions, as implemened for Java in the Kijaro project - http://kijaro.dev.java.net

left by Stephen Colebourne . Saturday, 8 November 2008 6:35 AM

You can always use commons-collections with generics (http://sourceforge.net/projects/collections).

left by James Carr . Saturday, 8 November 2008 4:38 AM

no, we can't. go clean up your room.

left by momma . Thursday, 6 November 2008 10:35 PM

heh...I was looking for exactly that a few weeks ago, after working with FileFilter

left by Gible Fog . Thursday, 6 November 2008 11:46 AM
Add Comment