S13L04 – Comparator using Lambda continues


Comparator Interface with Lambda in Java

Table of Contents

1. Introduction

In this article, we will explore the use of the Comparator interface with lambda expressions in Java. The Comparator interface allows developers to define custom sorting logic for collections of objects. By combining it with lambda expressions, introduced in Java 8, we can write more concise and readable code.

This article is designed for beginners and intermediate Java developers who have basic knowledge of Java collections and lambda expressions. We will provide a step-by-step explanation of the code and demonstrate how to apply the Comparator interface using a lambda to sort a list of custom objects based on specific criteria.

2. Understanding the Comparator Interface

The Comparator interface in Java is used to order the objects of a user-defined class. It provides a compare method that allows us to define custom logic for sorting objects. Here’s a simple explanation:

  • compare(T o1, T o2): Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

When combined with lambda expressions, this becomes much more powerful and allows us to write inline sorting logic with fewer lines of code.

Feature Comparator
Functionality Custom sorting logic
Lambda compatibility Yes
Use case Sorting objects based on properties

3. Lambda Expressions in Java

Lambda expressions provide a clear and concise way to represent an anonymous function (i.e., a function without a name) and can be used primarily to define the implementation of functional interfaces. Lambda expressions are particularly useful when dealing with collections and custom sorting logic.

Syntax:

For example:

4. Using the Comparator Interface with Lambda

Now, let’s take a look at the practical implementation of using the Comparator interface with lambda expressions. In this project, we have a list of custom Data objects that we want to sort based on the length of their names.

Program Code:

5. Code Walkthrough and Explanation

Let’s break down the code step by step:

  1. Class Definition:
    • We have a Data class with a single attribute name.
    • It contains a constructor, getter method for name, and an overridden toString() method to provide a meaningful string representation of the Data object.
  2. List Initialization:
    • A list of Data objects is created and populated with names like “John”, “Gia”, “Chaand”, “Pooja”, and “Ed”.
  3. Sorting Logic:
    • We use Collections.sort() to sort the list based on the length of the names. This is achieved by passing a lambda expression that compares the length of name in two Data objects (o1 and o2).
    • If o1‘s name is shorter than o2‘s, it returns -1. If it’s longer, it returns 1. If both names are of equal length, it returns 0.
  4. Output:
    • The sorted list is printed, showing the order of the names based on their length.

Expected Output:

6. Conclusion

In this article, we explored how to use the Comparator interface with lambda expressions in Java to sort a list of custom objects. The use of lambda expressions makes the code cleaner and easier to maintain. This approach can be applied to various scenarios where custom sorting logic is required, especially in real-world applications involving complex data structures.