Empower the Team

…or why sometimes 1 + 1 > 2

lambdaj proves Java programs can be concise and even more elegant – in a functional kind of way

leave a comment »

Is the Java programming language too verbose? lambdaj clearly proves the answer is NO.

Java programs usually are, because Java APIs (starting with the SDK) have built a poor reputation for the language.

This powerful library might help us change that in the future.

In a nutshell,  lambdaj allows you to manipulate collections in a pseudo-functional and statically typed way.

This means you can reduce your stereotypical and superfluous ways of looping in Java to equivalent one-liners that are:

  • much easier to understand (thus allowing you to focus on the actual problem not on details)
  • type safe, being written in Java benefits from static typing (and all the good things that derive from here – clear semantics, compiler checks, tool support in refactoring)
  • concise, less error prone, giving you more power per line of code

Below I pasted some usage examples from their site.
I did not add any explanations, because I’m sure you’ll understand what’s going on straight away:

List<Person> personsInFamily = asList(new Person("Domenico"),
                              new Person("Mario"), new Person("Irma"));
forEach(personsInFamily).setLastName("Fusco");
List<Person> sortedByAgePersons = sort(personsInFamily, on(Person.class).getAge());

List<Integer> biggerThan3 = filter(greaterThan(3), asList(1, 2, 3, 4, 5));

List<Integer> ages = extract(personsInFamily, on(Person.class).getAge());

Map<String, Person> personsByName = index(personsInFamily,
            on(Person.class).getFirstName());

Person me = new Person("Mario", "Fusco", 35);
Person luca = new Person("Luca", "Marrocco", 29);
Person biagio = new Person("Biagio", "Beatrice", 39);
Person celestino = new Person("Celestino", "Bellone", 29);
List<Person> meAndMyFriends = asList(me, luca, biagio, celestino);
Group<Person> group = group(meAndMyFriends, by(on(Person.class).getAge()));
List<Person> oldFriends = filter(having(on(Person.class).getAge(), greaterThan(30)),
            meAndMyFriends);

I really look forward to use this powerful tool!

Written by dyancorutiu

May 21, 2009 at 12:46 am

Posted in refactoring, software development

Tagged with

Leave a Reply