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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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
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

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Building a REST Service That Collects HTML Form Data Using Netbeans, Jersey, Apache Tomcat, and Java
  • Recurrent Workflows With Cloud Native Dapr Jobs
  • Java Virtual Threads and Scaling
  • Java’s Next Act: Native Speed for a Cloud-Native World

Trending

  • Issue and Present Verifiable Credentials With Spring Boot and Android
  • Why We Still Struggle With Manual Test Execution in 2025
  • Evolution of Cloud Services for MCP/A2A Protocols in AI Agents
  • Unlocking AI Coding Assistants Part 1: Real-World Use Cases
  1. DZone
  2. Coding
  3. Java
  4. Using Lambda Expression to Sort a List in Java 8 using NetBeans Lambda Support

Using Lambda Expression to Sort a List in Java 8 using NetBeans Lambda Support

As part of JSR 335Lambda expressions are being introduced to the Java language from Java 8 onwards and this is a major change in the Java language.

By 
Mohamed Sanaulla user avatar
Mohamed Sanaulla
·
Mar. 21, 13 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
345.7K Views

Join the DZone community and get the full member experience.

Join For Free

As part of JSR 335Lambda expressions are being introduced to the Java language from Java 8 onwards and this is a major change in the Java language. If you want to learn more about the what Lambda expressions and also about the JSR 335, you can visit the following resources:

  • Project Lambda on OpenJDK.
  • Lambda FAQ.
  • JSR 335′s draft specification.

If I go into each and every feature/changes made as part of JSR 335 then it will be confusing for me as well as you. So to make it easier to appreciate the changes, I will pick some use cases and try to apply the features added as part of the JSR. Before I begin you would have to download the Netbeans version which supports the lambda expression syntax as well as the JDK build which has the JSR 335 changes.

  • The JDK build which has the JSR 335 changes i.e the lambda expression support as well as the enhancements to the collections API can be downloaded from here.
  • The Netbeans build with experimental support for Lambda Expressions.


Once you have downloaded Netbeans nightly build zip, you will have to extract the contents and then navigate to the bin directory to launch Netbeans. And once you have downloaded and extracted the contents of the JDK with JSR335 support you would have to create a new platform in Netbeans from Tools -> Java Platforms menu in order to use the JDK with the JSR335 changes. The add Java Platform popup looks something like:
AddJavaP_Netbeans

Now lets consider a List with the following contents for our example:

List<Person> personList = new ArrayList<>();
personList.add(new Person("Virat", "Kohli"));
personList.add(new Person("Arun", "Kumar"));
personList.add(new Person("Rajesh", "Mohan"));
personList.add(new Person("Rahul", "Dravid"));

and lets use the pre Java 8 or the current approach to sorting this list based on the firstName:

//Sorting using Anonymous Inner class.
Collections.sort(personList, new Comparator<Person>(){
  public int compare(Person p1, Person p2){
    return p1.firstName.compareTo(p2.firstName);
  }
});

f you are using the Netbeans nightly build which supports Lambda expressions, then the IDE will provide an hint which says:
Netbeans_Lambda_Hint1

and then using the Netbeans Support to replace the above code with a Lambda expression we get:

//Anonymous Inner class replaced with Lambda expression.
Collections.sort(personList, (Person p1, Person p2) -> p1.firstName.compareTo(p2.firstName));

One can see the amount of verbosity which has been reduced by using the Lambda expressions. And also the code is much more clearer now than it was when we used Anonymous inner classes. One can make it even more concise by removing the type information from the parameters as the type information is inferred from the context in which the lambda expression is being used.

//Lambda expression with type information removed.
Collections.sort(personList, (p1, p2) -> p1.firstName.compareTo(p2.firstName));

Lets dissect the above lambda expression to understand its parts. The general syntax of a lambda expression is: () -> {} OR () -> a single statment/expression.
The ()-> {} version is used when the lambda expression’s body has to be a block and the other version is used when the lambda expression has a single statment/expression. In the above case there is a single expression and thus a block is not used here.
The () is used to declare the parameters to the lambda expression. The parameters can have the type information or can be skipped if the type can be inferred from the context. In our case the type information is inferred from its context.
Another observation is that the lambda expression is equivalent to overriding the compare method present in the Comparator class. And it also replaces the code which creates an Anonymous Inner class to extend the Comparator class.

Another change which can be made to the above code is that the “sort” method has been added to the List class as part of JSR 335 changes and we can make use of that method to sort the list:

//Using sort method in List.
personList.sort((p1, p2) -> p1.firstName.compareTo(p2.firstName));

Please note that the sort method is present in the List class which is part of the JDK with JSR 335 support.

This is just a sample example of using Lambda expressions in our existing code. For a more detailed information please visit the links shared by me at the beginning of the post.



 

Java (programming language) NetBeans Sort (Unix)

Published at DZone with permission of Mohamed Sanaulla, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building a REST Service That Collects HTML Form Data Using Netbeans, Jersey, Apache Tomcat, and Java
  • Recurrent Workflows With Cloud Native Dapr Jobs
  • Java Virtual Threads and Scaling
  • Java’s Next Act: Native Speed for a Cloud-Native World

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!