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.

Tagged with:
 
About The Author

Mohamed Sanaulla

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

  1. [...] and soothing to the eye. If you are not familiar with the idea of Lambda Expressions, check out my previous entry which covers in brief about Lambda [...]

  2. [...] of SAM interfaces is recreated and are called Functional interfaces. These can be represented using Lambda expressions, Method reference and constructor references(I will cover these two topics in the upcoming blog [...]

  3. [...] All of the methods except test(T t) are default methods and test(T t) is abstract. One approach of providing the implementation for this abstract method is to use Anonymous inner class and the other approach is to use lambda expression. [...]

  4. [...] the same. For those totally unaware of Lambda Expressions in Java, I would recommend them to read this first before getting into this [...]

  5. [...] 8 provides an excellent mechanism to achieve this using Lambda expressions. lets look at how it can be [...]

  6. Arun says:

    Nice article

    thanks,
    Arun

Leave a Reply