Core Java

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.

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
Subscribe
Notify of
guest

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
3 years 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. 

Back to top button