How to Sort Custom Objects in Java 8

1. Introduction

Java 8 introduced new features and enhancements, particularly in the area of collections and stream processing. Among these, sorting custom objects has become more straightforward and expressive, thanks to lambda expressions and the Stream API. In this guide, we will explore how to sort custom objects in Java 8 using these powerful features, specifically using a Product class as an example.

2. Program Steps

1. Define a custom Product class with attributes like productName and price.

2. Create a list of Product objects.

3. Sort the list by the productName attribute in ascending order.

4. Sort the list by the price attribute in ascending order.

3. Code Program

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

class Product {
    private String productName;
    private double price;

    public Product(String productName, double price) {
        this.productName = productName;
        this.price = price;
    }

    public String getProductName() {
        return productName;
    }

    public double getPrice() {
        return price;
    }

    @Override
    public String toString() {
        return "Product{" + "productName='" + productName + '\'' + ", price=" + price + '}';
    }
}

public class SortCustomObjects {
    public static void main(String[] args) {
        // 2. Create a list of Product objects.
        List<Product> products = new ArrayList<>();
        products.add(new Product("Laptop", 1000.99));
        products.add(new Product("Mobile", 500.49));
        products.add(new Product("Tablet", 300.29));

        // 3. Sort the list by the productName attribute in ascending order.
        products.sort(Comparator.comparing(Product::getProductName));
        System.out.println("Sorted by product name: " + products);

        // 4. Sort the list by the price attribute in ascending order.
        products.sort(Comparator.comparingDouble(Product::getPrice));
        System.out.println("Sorted by price: " + products);
    }
}

Output:

Sorted by product name: [Product{productName='Laptop', price=1000.99}, Product{productName='Mobile', price=500.49}, Product{productName='Tablet', price=300.29}]
Sorted by price: [Product{productName='Tablet', price=300.29}, Product{productName='Mobile', price=500.49}, Product{productName='Laptop', price=1000.99}]

4. Step By Step Explanation

1. We define a Product class with attributes productName and price. The class also includes getter methods and a toString() method for easy display.

2. We create a list of Product objects for sorting.

3. For sorting by product name, we use the Comparator.comparing method combined with method references. The expression Product::getProductName is a method reference that points to the getProductName() method of the Product class. This provides a concise way to specify the sorting key.

4. For sorting by price, we use the Comparator.comparingDouble method, which is a specialized version for double keys. We use a method reference Product::getPrice to specify the sorting key.

With Java 8's lambda expressions and method references, sorting custom objects, such as Product, becomes a more streamlined and readable process.

Comments