Blog Java 8 Features

Java 8 Empowerment: Real-World Applications of Consumers in Streamlining Data Operations

Let’s say you have a list of Product objects, and you want to perform different operations on these products based on certain conditions. You might want to apply one operation to products that meet specific criteria and a different operation to those that don’t. Using a Consumer, you can encapsulate these different operations and easily apply them as needed.

Here’s an example:

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

class Product {
    private String name;
    private double price;

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

    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }
}

public class Main {
    public static void main(String[] args) {
        List<Product> products = Arrays.asList(
            new Product("Laptop", 1000),
            new Product("Phone", 700),
            new Product("Tablet", 500)
        );

        // Define different consumers for different operations
        Consumer<Product> discountConsumer = product -> {
            double discountedPrice = product.getPrice() * 0.9; // Apply 10% discount
            System.out.println("Applying discount to " + product.getName() + ": $" + discountedPrice);
        };

        Consumer<Product> normalPriceConsumer = product -> {
            System.out.println("No discount applied to " + product.getName() + ": $" + product.getPrice());
        };

        // Apply different operations based on product price
        products.forEach(product -> {
            if (product.getPrice() > 800) {
                discountConsumer.accept(product); // Apply discount for expensive products
            } else {
                normalPriceConsumer.accept(product); // Keep normal price for other products
            }
        });
    }
}

In this example:

  • We define two different Consumer instances: discountConsumer and normalPriceConsumer, each representing a different operation to be performed on Product objects.
  • We use a lambda expression to specify what each Consumer does.
  • Within the forEach loop, we check each product’s price and apply the appropriate Consumer to it based on the condition (in this case, whether the price is greater than $800).
  • Using accept(), we execute the appropriate Consumer for each product.

This approach allows for better organization of code, as each operation is encapsulated within its own Consumer. It also offers flexibility, as you can easily add or modify operations without changing the iteration logic.

Avatar

Neelabh

About Author

As Neelabh Singh, I am a Senior Software Engineer with 6.6 years of experience, specializing in Java technologies, Microservices, AWS, Algorithms, and Data Structures. I am also a technology blogger and an active participant in several online coding communities.

You may also like

Blog Design Pattern

Understanding the Builder Design Pattern in Java | Creational Design Patterns | CodeTechSummit

Overview The Builder design pattern is a creational pattern used to construct a complex object step by step. It separates
Blog Tech Toolkit

Base64 Decode

Base64 encoding is a technique used to encode binary data into ASCII characters, making it easier to transmit data over