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

Result:

comedy Recreators: 5


4. Specify search criteria code in an anonymous class

In this used case, an anonymous class was suggested since you are using a local class with just one method. You won't be using the class name in the outside. So, to reduce it further, an anonymous class should be used.

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

import java.util.Arrays;
import java.util.List;
class PracticingLambda {
interface AnimeSearchCriteria{
boolean testCriteria(Anime anime);
}

public static void displayWatchAnime2(List<Anime> list, AnimeSearchCriteria criteria){
for(Anime anime: list){
if(criteria.testCriteria(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();

PracticingLambda.displayWatchAnime2(animeList, new AnimeSearchCriteria() {
@Override
public boolean testCriteria(Anime anime) {
return 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


5. Specify search criteria code in a lambda expression

Using lambda expression instead of anonymous class can reduce the code much further.

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

import java.util.Arrays;
import java.util.List;
class PracticingLambda {

interface AnimeSearchCriteria{
boolean testCriteria(Anime anime);
}

public static void displayWatchAnime2(List<Anime> list, AnimeSearchCriteria criteria){
for(Anime anime: list){
if(criteria.testCriteria(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();

PracticingLambda.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();
}
}

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

Result:

comedy Recreators: 5

The highlighted code above is an example of lambda expression. Even though, we haven't touch lambda yet but still we can compare the code in the anonymous class and the lambda. So, what we reduce when using lambda is the name of the interface, the name of the method and keyword return; There are also some stuffs like overrides and braces. And what is left was the parameter object with an arrow pointing to the statements.


6. Used standard functional interfaces with lambda expression

    The above lambda is a functional interface. A functional interface is an interface with one abstract method and an option of zero or more default methods;

Note:

This takes too long... I will put the other used case in the next chapter...

Java ProgrammingWhere stories live. Discover now