Home > Back-end >  Can somebody please explain me the lambda expressions part of my code? [closed]
Can somebody please explain me the lambda expressions part of my code? [closed]

Time:10-02

I have an interface called Rushable

import com.acme.utils.MyDate;

public interface Rushable {
    public abstract boolean isRushable(MyDate orderDate, double amount);
}

Then I have a class called Orders. The class has a bunch of method, getters and setters that is not required for the sake of this question.

import com.acme.utils.MyDate;

public class Order 
{
    private MyDate orderDate;
    private double orderAmount = 0.00;
    private String customer;
    private Product product;
    int quantity;
    public static double taxRate;
    
    private static Rushable rushable;
    public static Rushable getRushable()
    {
     return rushable;
    }
    public static void setRushable(Rushable rushable)
    {
     Order.rushable = rushable;
    }
    
    public boolean isPriorityOrder()
    {
    boolean priorityOrder = false;
    if( rushable != null ) {
        priorityOrder = rushable.isRushable(orderDate, orderAmount);
        }
    return priorityOrder;
    }
}

To test the class Orders, I have another class called TestOrders

import com.acme.domain.Order;
import com.acme.utils.MyDate;


public class TestOrders {

    public static void main(String[] args) 
    {
        MyDate date1 = new MyDate(20,1,2008);
        Solid s1 = new Solid("Acme Anvil", 1668, 0.3, UnitOfMeasureType.CUBIC_METER, false, 500, 0.25, 0.3);
        Order anvil = new Order(date1, 2000.00, "Wile E Coyote", s1, 10);
        MyDate date2 = new MyDate(10,4,2008);
        Solid s2 = new Solid("Acme Balloon", 1401, 15, UnitOfMeasureType.CUBIC_FEET, false, 10, 5, 5);
        Order balloons = new Order(date2, 1000.00, "Bugs Bunny", s2, 125);
        

        Order.setRushable((orderDate, orderAmount) -> orderAmount>1500);
        System.out.println("Anvil isPriorityOrder: "   anvil.isPriorityOrder());
        System.out.println("Balloons isPriorityOrder: "   balloons.isPriorityOrder());

    }
    
}

Solid and MyDate are another classes used to store the fields of class Order.

The problem I am having is with the part of the code in the TestOrders class

Order.setRushable((orderDate, orderAmount) -> orderAmount>1500);

and in the following part of the Order class

public static void setRushable(Rushable rushable)
    {
     Order.rushable = rushable;
    }

    public boolean isPriorityOrder()
    {
    boolean priorityOrder = false;
    if( rushable != null ) {
        priorityOrder = rushable.isRushable(orderDate, orderAmount);
        }
    return priorityOrder;
    }

I am trying to understand these two parts of the code for the entire day. I'd be grateful if anyone can explain it to me.

CodePudding user response:

This is lambda expression with Functional Interfaces. So you are defining the function for isRushable that needs to be executed, when the actual parameters are passed. The older way would be to implement the class and set its object. With lambda the code becomes very concise.

During the method call isPriorityOrder you are passing the actual parameters with which the function gets executed. In this case the function says to return true when amount > 1500, so following will get printed

Anvil isPriorityOrder: true (amount=2000.00 which is > 1500) Balloons isPriorityOrder: false (amount=1000.00 which is < 1500)

CodePudding user response:

you need look for funcional interfaces in java

Funcional interface are interface contains only one method ( to be implemented )

Creating 2 Funcional interface :

public interface FunctionInterfaceReturnVoid {
    public abstract void doingAnything(String s); 
}


@FunctionalInterface  // not required but suggested to notate on functional interfaces
public interface FunctionInterfaceReturnString {
    
    public abstract String doingAnythingAndReturn(String s);

    // public abstract String secondMethodInFuncionalInterface(); // this will go wrong for I have added @FunctionalInterface
}

we can creating method to process our interfaces. their are using our interface but the implementation will be created dinamically code on lambda expression

private static String runFunctionInterfaceReturnString(String string, FunctionInterfaceReturnString func){
    return func.doingAnythingAndReturn(string);
}

private static void runFunctionInterfaceReturnVoid(String string, FunctionInterfaceReturnVoid func){
    func.doingAnything(string);
}

using our methods


        
    public static void main(String[] args) {

        // using FunctionInterfaceReturnString implement with lambda on paramters
        String replacingE = runFunctionInterfaceReturnString("test", s -> s.replace("e", ""));
        String replacingT = runFunctionInterfaceReturnString("test", s -> s.replace("t", ""));

        System.out.println(replacingE); // tst
        System.out.println(replacingT); // es

        // using FunctionInterfaceReturnString implement with lambda in a var
        FunctionInterfaceReturnString implementationInVar = s -> s.replace("t", "").concat("asd").replace("a", "b");

        String doingManyThings = runFunctionInterfaceReturnString("test", implementationInVar);

        System.out.println(doingManyThings); // esbsd


        // using FunctionInterfaceReturnVoid implement with lambda on paramters
        runFunctionInterfaceReturnVoid("test", s ->  System.out.println(s) ); // will print "test"

        // using FunctionInterfaceReturnVoid implement with lambda with method reference
        runFunctionInterfaceReturnVoid("test", System.out::println); // same code above with method reference ( will print "test" )

    }

  •  Tags:  
  • java
  • Related