An example of Lambda Expression with Generics in Java
Lambda Expression is an important feature of contemporary programming languages. It has been available in Java since version 8. There are many reasons that Lambda is useful. Here is an example of practical use of Lambda with Generics.
There are often scenarios that we want to create a simple function quickly to apply to a variable. We don't want to scroll way up or down the screen or go to another file to create a function there. To quickly create a function for immediate use, we want to write it in the space right below the variable. In the past, we could do so by an Anonymous Class that its syntax is quite awkward. Now with Lambda, we can write a simple function, right below a variable, with clear and intuitive syntax.
Earlier I provided with examples of array operations with Generics. I will reuse the same arrays of numbers in the previous posts.
The above example shows that I have an Integer array variable called iNumArray. I wanted to sum up all the numbers in the array. I wrote the sumSold function in Lambda immediate below the variable.
Similarly, I have a Double array variable called dNumArray. I wanted to find the maximum number in that array. I wrote the findMax function in Lambda immediate below the variable.
Lambda can be used with or without Generics. In this example Generics is used. The Class declared in the Lambda functions has to be an extension of the Number class. That is based on the bounded generic class defined in the Interface called ArrayNum.
The above program produces this result:
Total number of houses sold in the year: 85
Highest price in the first month: 1210000.0
There are often scenarios that we want to create a simple function quickly to apply to a variable. We don't want to scroll way up or down the screen or go to another file to create a function there. To quickly create a function for immediate use, we want to write it in the space right below the variable. In the past, we could do so by an Anonymous Class that its syntax is quite awkward. Now with Lambda, we can write a simple function, right below a variable, with clear and intuitive syntax.
Earlier I provided with examples of array operations with Generics. I will reuse the same arrays of numbers in the previous posts.
import java.util.Arrays; interface ArrayNum <T extends Number>{ T calcFun (T[] array); } class LambdaGenericArrayMain { public static void main (String args[]){ Integer[] iNumArray = {5, 4, 5, 6, 8, 9, 9, 10, 9, 9, 6, 5}; ArrayNum<Integer> sumSold = (numArray) -> { var sum = 0; for (var num: numArray) sum += num; return sum; }; System.out.println ("Total number of houses sold in the year: " + sumSold.calcFun (iNumArray) ); Double[] dNumArray = {1120000d, 780000d, 890000d, 910000d, 1210000d}; ArrayNum<Double> findMax = (numArray) -> { Double[] list = numArray.clone(); Arrays.sort (list); return list[list.length-1]; }; System.out.println ("Highest price in the first month: " + findMax.calcFun (dNumArray) ); } }
The above example shows that I have an Integer array variable called iNumArray. I wanted to sum up all the numbers in the array. I wrote the sumSold function in Lambda immediate below the variable.
Similarly, I have a Double array variable called dNumArray. I wanted to find the maximum number in that array. I wrote the findMax function in Lambda immediate below the variable.
Lambda can be used with or without Generics. In this example Generics is used. The Class declared in the Lambda functions has to be an extension of the Number class. That is based on the bounded generic class defined in the Interface called ArrayNum.
The above program produces this result:
Total number of houses sold in the year: 85
Highest price in the first month: 1210000.0
Comments
Post a Comment