Home » Java » Core Java » Lambda Expressions Before And After Java 8

About Venkatesh Nukala

Venkatesh Nukala
Venkatesh Nukala is a Software Engineer working for Online Payments Industry Leading company. In my free time, I would love to spend time with family and write articles on technical blogs. More on JavaProgramTo.com

Lambda Expressions Before And After Java 8

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.

1. Introduction

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

In this article, We will see the example programs on sorting using Comparator, File names validation, Retrieving only hidden files and filtering list objects based on conditions.

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.

1
2
3
4
5
Collections.sort(emplyeesList, new Comparator() {
 public int compare(Employee a1, Employee a2){
  return a1.getId().compareTo(a2.getId());
 }
});

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.

1
emplyeesList.sort(Comparator.comparing(Employee::getId));

Here, Comparator is a Functional Interface which has only one abstract method.

Comparator has a static method comparing(.Function.) which accepts only Funtion interface.

https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html

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.

1
2
3
4
5
File[] hiddenFiles = new File("directory_name").listFiles(new FileFilter() {
 public boolean accept(File file) {
  return file.getName().endsWith(".xml");
 }
});

3.2 In Java 8

The above code is simplified to single line which reduces the errors.

1
File[] hiddenFiles = new File("directory_name").listFiles( file -> file.getName().endsWith(".xml"));

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.

1
2
3
4
5
File[] hiddenFiles = new File("directory_name").listFiles(new FileFilter() {
 public boolean accept(File file) {
  return file.isHidden();
 }
});

4.2 In Java 8

Just using the new Method Reference concept as part of new Java 8.

1
File[] hiddenFiles = new File("directory_name").listFiles(File::isHidden);

5. Conclusion

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.

Published on Java Code Geeks with permission by Venkatesh Nukala, partner at our JCG program. See the original article here: Lambda Expressions Before And After Java 8

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
1 Comment Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mamta Sharma
51 minutes ago

What is the proxy design pattern in Java? The object oriented Proxy Design Pattern is a structural design pattern which is concerned with how classes and objects compose to form larger structures. The proxy design pattern in java takes a similar approach by providing a proxy object that acts as a placeholder for another object. Commonly a proxy is a class that functions as an interface to something else. The proxy design pattern can be best understood with the help of a real-world example. In computer networks, we usually come across the term proxy server.