TechnologyZer
technologyzer.com

Introduction to UnaryOperator Functional Interface

T apply(T t) is an abstruct method in Unary functional interface. It takes an input and return same type of result.

public class Product {
    private double price;

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

    public double getPrice() {
        return price;
    }

    public void applyDiscount(UnaryOperator<Double> discountFunction){
        this.price = discountFunction.apply(this.price);
    }
}

public class Main {
    public static void main(String[] args) {
        Product product = new Product(100);
        UnaryOperator<Double> discountFunction = (price)-> price*0.9;

        product.applyDiscount(discountFunction);
        System.out.println("Product value after discount: " + product.getPrice());
    }
}

Leave a Comment