Introduction
How many times have you read or written the same two or three lines of code that frequently seem to go together, and even though they operate on different objects, feel like the same thing? And how often these repetitions involve some sort of collections iteration or more generically manipulation? These repetitions in the code are something that developers eventually learn to filter out and ignore when reading code, once they figure out where the interesting parts are placed. But even if the developers get used to it, it slows them down. Code like that is clearly written for computers to execute, not for developers to read.
lambdaj is a library that makes it easier to address this issue by allowing to manipulate collections in a pseudo-functional and statically typed way. In our experience to iterate over collection, especially in nested loops, is often error prone and makes the code less readable. The purpose of this library is to alleviate these problems employing some functional programming techniques but without losing the static typing of Java. We impose this last constraint to make refactoring easier and safer and allow the compiler to do its job.
Access Collections Without Explicit Loops
The main purpose of lambdaj is to partially eliminate the burden to write (often nested and poorly readable) loops while iterating over collections. In particular, it allows to iterate collections in order to:
- filter its items on a given condition
- convert each item with a given rule
- extract a given property from each item
- sort the items on the values of one of their property
- group or index the items on the value of one or more properties
- invoke a method on each item
- sum (or more generally aggregate) the items or the values of one of their properties
- concatenate the
string
representation of the items or of the values of one of their properties
without having to write a single explicit loop.
How does lambdaj Work?
There are 2 ideas at the base of lambdaj. The first one is to treat a collection of objects as it was a single object by allowing to propagate a single method invocation to all the objects in the collection as in the following example:
List<Person> personInFamily = asList(new Person("Domenico"),
new Person("Mario"), new Person("Irma"));
forEach(personInFamily).setLastName("Fusco");
In this example, all the persons in the list belong to the same family so they all have the same last name. The forEach
method actually returns a proxy object that implements both the Iterable interface and all the methods in each object in the given list. That allows to invoke a method of the Person
object on the object returned by the forEach
method as it was an instance of the Person
class. When you do that, under the hood lambdaj propagates your invocation to all the objects in the collection.
The second idea on which lambdaj is built is the possibility to have a pointer to a Java method in a statically typed way by using the lambdajs on construct. That allows to easily and safely define on which argument a given lambdaj feature has to be applied. For example, in the following statement, we used the on
construct in order to say the argument (their respective ages) on which a list of persons has to be sorted:
List<Person> sortedByAgePersons = sort(persons, on(Person.class).getAge());
Comparing this last statement with the piece of code necessary to achieve the same result in plain Java:
List<Person> sortedByAgePersons = new ArrayList<Person>(persons);
Collections.sort(sortedByAgePersons, new Comparator<Person>() {
public int compare(Person p1, Person p2) {
return Integer.valueOf(p1.getAge()).compareTo(p2.getAge());
}
});
makes evident how lambdaj could improve both the productivity while writing code and probably even more important its readability when you are called to maintain it.
lambdaj Features
As stated, lambdaj is designed to easily manipulate collections. Its features are intended to filter, convert, index and aggregate the items of a collection without explicitly iterating on it. Moreover the lambdaj APIs are designed to be easily concatenated in order to jointly use two or more features in a single statement. To investigate these features in more details and possibly to download and start using lambdaj, check it out here.
History
- 14th June, 2009: Initial post