Lambda

4 0 0
                                    

Other Used Case:

6. Used standard functional interfaces with lambda expressions

    As stated before, function interfaces are interfaces with one abstract method (a method without a body and declared abstract) and can have zero or more default methods (we will go into this in the succeeding chapters). Knowing this, it is so easy for the developers to just prepare a set of library for this common functional interfaces just like in the previous example. And we can find that common functional interfaces that uses in the java.util.function package.

interface Predicate<T> { boolean test(T t);}This is just one example of that standard functional interface. T signifies as data type or the class name. Check the example below.

=========================

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
class OtherUsedCaseOfLambda {

public static void displayWatchAnime2(List<Anime> list, Predicate<Anime> criteria){
for(Anime anime: list){
if(criteria.test(anime)){
System.out.println(anime.animeGenre + " " + anime.animeName + ": " + anime.watchedTimes);
}

}
}

public static void main(String[] args){
Anime[] animeArray = {
new Anime("Danmachi", 6, "Action"),
new Anime("Naruto", 3, "Action"),
new Anime("Recreators", 5, "Comedy")
};
List<Anime> animeList = Arrays.stream(animeArray).toList();

OtherUsedCaseOfLambda.displayWatchAnime2(animeList,
(Anime anime) -> anime.watchedTimes >= 2 &&
anime.watchedTimes <= 10 &&
anime.animeGenre.equals("comedy"));
}
}
class Anime{
String animeName;
int watchedTimes;
String animeGenre;

public Anime(String name, int times, String genre){
animeName = name;
watchedTimes = times;
animeGenre = genre.toLowerCase();
}
}

=========================

Results:

comedy Recreators: 5

So, in the above code, the interface name was just different and it has angled brackets where you will place your data type. Since this interface is already defined in the library, all you need to do is to import it so that you can just invoke this standard function interface and its one abstract method.

7. Used lambda application through out your application

    Since it is a library of functional interfaces, we can more interface in there which we can implement in our program. Like for example the Consumer<T> 

interface Consumer<T>{

   void accept(T t);

}

As long as we satisfy the condition, we can used this functional interface. This Consumer has one abstract method. It accepts an object as a parameter and returns nothing. And we have that in our display method.

==========================

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
class OtherUsedCaseOfLambda {

public static void displayWatchAnime2(List<Anime> list, Predicate<Anime> criteria, Consumer<Anime> display){
for(Anime anime: list){
if(criteria.test(anime)){
display.accept(anime);
}
}
}

Java ProgrammingWhere stories live. Discover now