DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. Manipulating Collections Without Loops With lambdaj

Manipulating Collections Without Loops With lambdaj

Mario Fusco user avatar by
Mario Fusco
·
Jun. 23, 09 · Interview
Like (0)
Save
Tweet
Share
23.36K Views

Join the DZone community and get the full member experience.

Join For Free

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 is 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 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 property
    * concatenate the string representation of the items or of the values of one of their property

without 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 belongs 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 on is the possibility to have a pointer to a java method in a statically typed way by using the lambdaj's 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 iterate on it. Moreover the lambdaj API 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 at:

http://code.google.com/p/lambdaj/

Object (computer science)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Create a CLI Chatbot With the ChatGPT API and Node.js
  • Old School or Still Cool? Top Reasons To Choose ETL Over ELT
  • Integrate AWS Secrets Manager in Spring Boot Application
  • Building the Next-Generation Data Lakehouse: 10X Performance

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: