Instead of using Function<Anime, String>, java used <T, R> as a data type to be used by an interface Function; So, by using a generic, you can just substitute any class from as a substitute for the data type.
========================
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
class OtherUsedCaseOfLambda {
public static <X> void displayWatchAnime2(
Iterable<X> source,
Predicate<X> criteria,
Consumer<X> display){
for(X x: source) {
if (criteria.test(x)) {
display.accept(x);
}
}
}
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 > 3,
(Anime anime) -> anime.displayAnime());
}
}
class Anime{
String animeName;
int watchedTimes;
String animeGenre;
public Anime(String name, int times, String genre){
animeName = name;
watchedTimes = times;
animeGenre = genre.toLowerCase();
}
void displayAnime(){
System.out.println(animeGenre + ": " + animeName + " " + watchedTimes);
}
}
========================
Result:
action: Danmachi 6comedy: Recreators 5
The above code declared a generic type by specifying the generic to use which <X> before the return type void in the method.
9. Used aggregate operations that accepts lambda as parameters
All the functions we have used so far in standard function interface are the following:
Standard funtional Interface => Aggregate equivalent
a. Iterable<X> source => stream()
b. Predicate<X> filter = filter(Predicate<X> p)
c. Function<X, Y> map => map(Function<X, Y> f)
d. Consumer<Y> display => forEach(Consumer<Y> c)
We those functional interface in the java library but there is an aggregate operations which uses those standard functional interface too. Above are the equivalent in usage for each.
An aggregate is an operation combined to come up with the desired result. It starts with a stream which is a source of information. Usually we used list but this list is just a group or a collection and not a stream. So, what we do is to put those list in a stream. A stream can be imagined as a stream of water flowing constantly. And a list can be imagined as a bucket of water. So, if you pour that bucket in the stream, it will be part of the stream. Then, when the list is in the stream, this is where you used the aggregate operations to get what you want from the list.
=====================
import java.util.Arrays;
import java.util.List;
class OtherUsedCaseOfLambda {
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();
animeList
.stream()
.filter(anime -> anime.watchedTimes > 3)
.map(anime -> anime.getAnimeName())
.forEach(anime -> System.out.println(anime));
}
}
class Anime{
String animeName;
int watchedTimes;
String animeGenre;
public Anime(String name, int times, String genre){
animeName = name;
watchedTimes = times;
animeGenre = genre.toLowerCase();
}
String getAnimeName(){
return animeName;
}
}
=====================
Result:
DanmachiRecreators
The java library contains lots of prepared programs to make our life easy as a programmer. All we need to do is to explore the dungeon. Hehehe.
YOU ARE READING
Java Programming
Randomhttps://docs.oracle.com/javase/tutorial/ This is the link where I learned my java lessons online. I choose java as first programming language. I am a career shifter. I just want to start something new. I choose to write it here in Wattpad so that I...
Lambda
Start from the beginning
