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

Manipulating Collections With a Fluent Interface

Mario Fusco user avatar by
Mario Fusco
·
Jul. 19, 10 · Interview
Like (0)
Save
Tweet
Share
9.02K Views

Join the DZone community and get the full member experience.

Join For Free

Lambdaj is a Java library that allows you to manipulate collections in a declarative way. In particular its API is designed to be easily combined in more complex single statements as in the following example:

List<Person> fastCarsOwnersSortedByAges = sort(
extract(
select(cars, having(on(Car.class).getSpeed(), greaterThan(200)))
), on(Car.class).getOwner()
), on(Person.class).getAge());
Here a list of Cars is filtered in order to select only the ones faster than 200 km/h. Then the owners of all of these cars are extracted and in the end the resulting Persons are sorted based on their ages. The select(), extract() and sort() methods are all static methods so they can be used with a simple static import. This single statement looks quite powerful, especially if you compare what you should do to achieve the same result in plain Java:
List<Person> fastCarsOwnersSortedByAges = new ArrayList<Person>();
for(Car car : cars) {
if (car.getSpeed() > 200) {
fastCarsOwnersSortedByAges.add(car.getOwner());
}
}
Collections.sort(fastCarsOwnersSortedByAges, new Comparator<Person>() {
public int compare(Person p1, Person p2) {
return p1.getAge() - p2.getAge();
}
});
Anyway the lambdaj version can be a bit difficult to be read due to its nested form. That's why the release 2.3 of the library introduced the so called LambdaCollections that allows developers to use the same features in a more readable way:
List<Person> fastCarsOwnersSortedByAges = with(cars)
.retain(having(on(Car.class).getSpeed(), greaterThan(200)))
.extract(on(Car.class).getOwner())
.sort(on(Person.class).getAge());

These special collections implement a fluent interface providing a more handy way to employ lambdaj's features. In more detail, the with() static method wraps a plain list (set, map or array) with the corresponding lambda collection.

Note that the LambdaCollections actually implement the corresponding Java interface (i.e. LambdaList implements java.util.List) so you can use them in all other APIs as usual. This is done by simply delegating to the original wrapped collection for the methods belonging to the base interface, so you don't have to pay any performance penalty when invoking these methods.

Thus the LambdaCollections seamlessly enrich the API of the traditional Java Collections Framework with a fluent interface that allows you to leverage lambdaj's feature in a more comfortable way. The invocations of the methods of this fluent interface also change the state of the original wrapped collection. This implementation choice has been driven by 2 reasons:

  1. While to leave the original collection unchanged is closer to the functional programming principles, it didn't make too much sense to enforce an immutable API on an object that is however widely mutable by invoking the methods of the collection framework API.
  2. By never changing the instance of the wrapped collection assures that its characteristic are always reflected even by the wrapping lambdaj counterpart. It means, for example, that if you are wrapping a SortedSet with a LambdaSet, the items in this last Set are always guaranteed of staying sorted with the same criteria defined in the original Set.

Of course if you need to leave unchanged the original collection (i.e. when it is a list of JPA entities and you don't want to accidentally cascade delete entities from the database) it is enough to clone it before to use the other fluent methods as it follows:

with(cars).clone().remove(...) ...
Fluent interface Interface (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A Beginner's Guide to Infrastructure as Code
  • Keep Your Application Secrets Secret
  • Kubernetes-Native Development With Quarkus and Eclipse JKube
  • Stop Using Spring Profiles Per Environment

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: