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:
- Project Lambda on OpenJDK.
- Lambda FAQ.
- Another blog post which deals with the lambda expression support in detail
- JSR 335?s draft specification.
- 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.
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.
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:
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:
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.
Source : feedproxy[dot]google[dot]com
No comments:
Post a Comment