Lamba Expressions: Generic Functional Interface Tutorial

A Functional Interface can be generic with type parameters. If you are unfamiliar with generic type parameters and bounded types, then please watch my generic tutorials to familiarize yourself with the concepts. In this tutorial I will introduce you to the powerful things you can accomplish when combining generics with lambda expressions.
An example of a generic functional interface looks like this:
interface FunctionalInterface<T> {
        int mysteryMethod(T x);
// implicitly abstract
}
You can also have generic return types for your abstract method with a generic functional interface.
interface FunctionalInterface<T, V> {
        T mysteryMethod(V x);
// implicitly abstract
}



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 LambdaGenericFI
C:\Java>cd LambdaGenericFI
C:\Java\LambdaGenericFI>Notepad LambdaGenericFI.java

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


class LambdaGenericFI {
    public static void main(String args[]) {
	
        Integer iArray[] = new Integer[20];
        for(int i = 0; i < iArray.length; i++) { 
            iArray[i] = i;	
        }
		
        FunctionalInterface<Boolean, Integer> numbers = (y) -> y % 2 == 0;
        for(Integer temp : iArray) {
            if (numbers.oneParameter(temp))
                System.out.print(temp + " ");
        }
        System.out.println();

        numbers = (y) -> y % 2 != 0;
        for(Integer temp : iArray) {
            if (numbers.oneParameter(temp))
                System.out.print(temp + " ");
        }
        System.out.println("\n");

        FunctionalInterface<String, Integer[]> evenString =  (x) -> {
            String s = "";
            for(Integer temp: x) {
                if(temp % 2 == 0)
                    s += temp + " ";
            }
            return s;
        };
        System.out.println(evenString.oneParameter(iArray));

        FunctionalInterface<String, Integer[]> oddString =  (x) -> {
            String s = "";
            for(Integer temp: x) {
                if(temp % 2 != 0)
                    s += temp + " ";
            }
            return s;
        };

        System.out.println(oddString.oneParameter(iArray));
    }
}

interface FunctionalInterface<T, A> {
    T oneParameter(A param1);    
}

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


C:\Java\LambdaGenericFI>javac LambdaGenericFI.java
C:\Java\LambdaGenericFI>java LambdaGenericFI
0 2 4 6 8 10 12 14 16 18
1 3 5 7 9 11 13 15 17 19

0 2 4 6 8 10 12 14 16 18
1 3 5 7 9 11 13 15 17 19


Final thoughts

After watching this tutorial, are you left with a sense of confusion or frustration? If so, that is normal - you are learning. You will need to review my generics and lambda tutorials until each concept makes complete sense. Sometimes you may want to step away for a day and then come back to let new concepts "sink in". However, if this tutorial makes 100% sense and has you thinking about the power of combining generics with lambda expressions, then you can rest assured that you are ready to tackle even more advanced concepts. At this point in my tutorials, I plan on spending some time "filling in the gaps" on many things that I have skipped or overlooked throughout my tutorial series thus far. One of my goals was to cover some basics on generics and lambda expressions prior to teaching Collection, List, and ArrayList related stuff.


Tutorials