try {} catch ()

Java, Agile, the Web and other nice things

Nov

17

lambdaj – easy collection manipulation

By Olivier Gérardin

LambaJ is quite an interesting project. How many times did you write the same loop, with only a few differences that were impossible to factor out because of the lack of closures in Java?

LambdaJ partially alleviates the need for closures by allowing the manipulation of Collections in a way that doesn’t require iterating through the items. For example you could write

forEach(personInFamily).setLastName("Fusco");

with personInFamily being a List of Person, and setLastName being a method on the Person class. Actually, forEach returns a proxy that implements both the Iterable interface and the methods of the collection items’ class (Person in this case). A call to any of these methods is then propagated to each member of the collection.

As you can see, LambdaJ uses the DSL-style notation that mock frameworks introduced, which I’m not particularly fond of; but I must say that in this case it produces quite elegant and readable code.

As a bonus, LambaJ also offers its own flavor of closures

lambdaj – Project Hosting on Google Code

Leave a comment