Using Lambda Expression to sort a List in Java 8 using Netbeans Lambda Support

As part of JSR 335 Lambda 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:

 
 
 
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);
  }
});

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

Reference: Using Lambda Expression to sort a List in Java 8 using Netbeans Lambda Support from our JCG partner Mohamed Sanaulla at the Experiences Unlimited blog.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You'll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You'll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


+ four = 9



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books