Illuminatiiiiii
11/7/2018 - 12:39 AM

Generic Functional Interfaces & Lambdas

For Episode 63 of Java Series: https://youtu.be/wf2ZM2uA9xE

interface Thing<A>{ //Generic Functional Interface
    A thingMethod(A a); //Type parameter is used as both the return type and the method parameter
}
public class Main {

    public static void main(String[] args) {

        //Lambda Implementation using a String
        Thing<String> reverse = (String string) -> { //Must specify what type will be used with that object reference
            String reversedString = "";
            for (int i = string.length() - 1;i >= 0;i--){
                reversedString = reversedString + string.charAt(i);
            }
            return reversedString;
        };
        System.out.println(reverse.thingMethod("booger"));

        //Lambda Implementation using a Integer
        Thing<Integer> findfactorial = (number) -> {
            int result = 1;
            for (int i = 1; i <= number;i++){
                result = i * result;
            }
            return result;
        };
        System.out.println(findfactorial.thingMethod(5));

    }
}