R.S.
R.S.

Reputation: 377

In java, how can i declare a variable inside an if lambda condition?

In my code, i have a predicate method that returns i if certain conditions are met, and i need to declare a variable inside it, but the method is written in lambda, and i don't know how to do this. Here's my code:

public static Predicate<MFDWHDealExt> isAccepted() {
        MyCalendar startDate = new MyCalendar();

        return (i -> 
                i.getCompany().equals("ACCEPTED")
                && i.getDealType() == MFDealTypeEnum.AcceptedDeal              
                && i.getSettlementDate().beforeOrEqual(startDate.findDay(i.getDealDate(), 2, true))
                );
    }

i need to declare startDate (a custom date MyDate) with the value of i.getSettlementDate, otherwise the method findDay(that returns working days), because right now is null.

Upvotes: 4

Views: 1430

Answers (4)

Sergey
Sergey

Reputation: 947

Just use code block in lambda and declare your variable inside.

public static Predicate<MFDWHDealExt> isAccepted() {
    MyCalendar startDate = new MyCalendar();

    return (i -> {
            MyDate startDate = i.getSettlementDate();
            boolean result = i.getCompany().equals("ACCEPTED")
            && i.getDealType() == MFDealTypeEnum.AcceptedDeal              
            && startDate.beforeOrEqual(startDate.findDay(i.getDealDate(), 2, true));
            return result;
            });
}

Upvotes: 4

GhostCat
GhostCat

Reputation: 140457

Here:

    return (i -> { 
            return i.getCompany().equals("ACCEPTED")
              && i.getDealType() == MFDealTypeEnum.AcceptedDeal              
              && i.getSettlementDate().beforeOrEqual(startDate.findDay(i.getDealDate(), 2, true))
            });

When not using the one line syntax of lambdas, like x-> 2*x, you need block braces. And you can't use that one-line syntax when you intend to declare a local variable.

See here for the official documentation!

In other words: when you want to write several statements, like actually defining a local variable, then you need these braces!

Finally: be careful what you do here. (imho) Lambda expressions should be really short and concise. When they become "more complicated" (like: they need local variables), then I suggest to not use a lambda, but to declare a real method and call that.

Upvotes: 5

talex
talex

Reputation: 20455

You need expand it to code block;

    return (i -> { 
            int myVar;
            return i.getCompany().equals("ACCEPTED")
               && i.getDealType() == MFDealTypeEnum.AcceptedDeal              
               && i.getSettlementDate().beforeOrEqual(startDate.findDay(i.getDealDate(), 2, true));
            });

Upvotes: 1

Davide Lorenzo MARINO
Davide Lorenzo MARINO

Reputation: 26926

A lambda expression is a shortcut for an anonymous class implementing an interface with a single method.

You can define anythink you like inside that unique method.

The lambda expression:

 (a, b) -> {
    // Your code here      
 }

is equivalent to:

 new MyInterface() {
    public ReturnType uniqueMethod(AType a, BType b) {
        // Exactly the same code here
    }
 }

Depeding on the kind of anonymous class derived from the interface that you need to write the ReturnType can be also void and the number of parameters can be 0, or any number of parameters (in this case two parameters of type AType and BType)

The following form of lambda expression:

(a, b) -> something

is another shortcut and is equivalent to:

   new MyInterface() {
       public ReturnType uniqueMethod(AType a, BType b) {
           // Same as before but with the additional keyword return
           return something;
       }
    }

In your case you have to apply the first form of lambda expression with the block { } and you can define any kind and number of variables inside the brackets

Upvotes: 3

Related Questions