3. Specify search criteria code in a local class

    In a case when you want to add more properties to test in your code and not just the number of times you watch, the above code is not that generic anymore. This only means that you need to create more generic methods but that will make your code less dependable. So, specifying the search criteria in a local class is a much better option.

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

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

public static void displayWatchAnime(List<Anime> list, int minTimes, int maxTimes, String genre){

class AnimeTester implements AnimeSearchCriteria{
@Override
public boolean testCriteria(Anime anime) {
return anime.watchedTimes >= minTimes && anime.watchedTimes <= maxTimes && anime.animeGenre.equals(genre.toLowerCase());
}

}

AnimeTester tester = new AnimeTester();
for(Anime anime: list){
if(tester.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.displayWatchAnime(animeList, 2, 10, "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

To make the code more generic, we need to use a local class but in the above, we just added some parameters and access it through a local class. After I checked it again, I noticed that in the tutorial they used an object in the parameter and used constants in the condition for the test criteria. It means that if they want to added more conditions in the criteria, they will just add it in the body of that method without changing the whole class or creating another method. So, below is the right code about used case 3.

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

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

public static void displayWatchAnime(List<Anime> list){

class AnimeTester implements AnimeSearchCriteria{
@Override
public boolean testCriteria(Anime anime) {
return anime.watchedTimes >= 2 && anime.watchedTimes <= 10 &&
anime.animeGenre.equals("comedy");
}

}

AnimeTester tester = new AnimeTester();
for(Anime anime: list){
if(tester.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.displayWatchAnime(animeList);
}
}
class Anime{
String animeName;
int watchedTimes;
String animeGenre;

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

Java ProgrammingWhere stories live. Discover now