Lambda Expressions: Code Block Tutorial

In my Introduction to Lambda Expressions tutorial I demonstrated a simple form of a lambda expression that evaluated a single expression. The structure for a lambda expression that evaluates a single expression looks like this:

Parameter List | Lambda Operator (Arrow Token) | Expression  
(x, y)                       ->                  x * y     
The return result of the above lambda expression is the value of x * y.

What if we need to execute multiple statements inside of our lambda expression and return a value based on some logic? We can do this by simply putting in a code block body to the right of the lambda operator.

Parameter List | Lambda Operator (Arrow Token) | Block Body  
(x, y)                       ->                  { if (x > y) return x; else return y; }   
 
The block body form of lambda expression must include a return statement that is a compatible return type matching the abstract method declared in the functional interface.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md LambdaBlock
C:\Java>cd LambdaBlock
C:\Java\LambdaBlock>Notepad LambdaBlock.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class LambdaBlock {
    public static void main(String args[]) {
        if (args.length == 0) {
            System.out.println("Please input character(s) argument for filtering - not case sensitive.");
            return;
        }
		
        String nameArray[] = { "Sam", "Larry", "Steve", "Mary", "Dan", "Shelly", "Adam", "Sheila" };
          
        // create a lambda expression to filter names based on starting characters 
        FunctionalInterface fi = (n, f) -> {
            if ( n.toUpperCase().substring(0, f.length()).equals(f.toUpperCase()) )
                return true;
            else 
                return false;
        };
         
        System.out.println("Names that begin with " + args[0] + ":");   
        for (String temp : nameArray) {
            if (fi.nameFilter(temp, args[0]))
                System.out.println(temp);
        }
        System.out.println();

        // let's change up the filter to check for any characters in the name
        fi = (n, f) -> {
            if ( n.toUpperCase().contains(f.toUpperCase()) )
                return true;
            else 
                return false;
        };

        System.out.println("Names that contain " + args[0] + ":");   
            for (String temp : nameArray) {
                if (fi.nameFilter(temp, args[0]))
                    System.out.println(temp);
        }        
    }
}

interface FunctionalInterface {
    boolean nameFilter(String name, String filter);
}

Now switch back to the command prompt (CMD) and type in javac LambdaBlock.java and press Enter.
Now type in java LambdaBlock and press Enter.


C:\Java\LambdaBlock>javac LambdaBlock.java
C:\Java\LambdaBlock>java LambdaBlock A
Names that being with A:
Adam

Names that contain A:
Sam
Larry
Mary
Dan
Adam
Sheila


Final thoughts

Hopefully this all makes sense, if not then please review my Introduction to Lambda Expressions tutorial and then come back to this one again.


Tutorials