Quick guide to few example programs on before and after Java 8. Java 8 Concepts further simplifies the code and profound for core logic rather than syntax.
In this tutorial, We’ll learn how to write programs using java 8
lambda and Stream concepts with examples. Many developers feel learning java 8 concepts may be hard to understand. But once we are good using them then feel reduces error prone code and improves the performance of the application.
Read article on Java 8 Lamda Expression Rules
2. Sorting – Lambda Expression
2.1 Before Java 8
See the below code which is implemented in older versions of Java. Here trying to sort the Employee’s based on the id column.
Here, written code in 5 lines in which includes Comparator implementation.
2.2 In Java 8
See the below code in Java 8. All the code is in single line and not seeing comparing logic.
Here, Comparator is a Functional Interface which has only one abstract method.
Comparator has a static method comparing(.Function.) which accepts only Funtion interface.
Note: Function<T, R> also a Functional Interface. This has a method apply(T t) which will be called from Comparator.comparing() method.
3. File Name Validation with Lambda Expression
3.1 Before Java 8
See the code in java old version. Here the core logic is
file.getName().endsWith(“.xml”); The remaining code is just syntax.
3.2 In Java 8
The above code is simplified to single line which reduces the errors.
Note: Java 8, just focus on the business logic and takes its syntax internally. Developer need not to worry about it.
4. Retrieving only Hidden files – Lambda Expression
4.1 Before java 8
File has a method to check the method is hidden or not using isHidden() method.
4.2 In Java 8
Just using the new Method Reference concept as part of new Java 8.
In this post, We’ve seen example lambda program before and after Java 8.
Observed the differences between them. Java 8 were in many ways more profound than any other changes to Java in its history.