Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package imLearning;
- import java.util.Arrays;
- import java.util.List;
- import java.util.function.*;
- public class muscle {
- public static void main(String[] args) {
- List<Anime> anime = Arrays.asList(
- new Anime("One Piece", "TBE", "Shounen", 10),
- new Anime("Naruto", "Trash", "Shounen", 1),
- new Anime("DragonBall", "Average", "Shounen", 6),
- new Anime("Attack on Titan", "Good", "Fantasy", 7),
- new Anime("Sword Art Online", "Trash", "Isekai", 2),
- new Anime("One Punch Man", "Good", "Shounen", 8),
- new Anime("Tokyo Ghoul", "Average", "Horror", 5)
- );
- // Method calling
- lowestAnimeRating(anime, 9);
- animeGenre(anime, "Shounen");
- // Without Lambdas
- printAnime(anime, new animeCondition() {
- @Override
- public boolean test(Anime a) {
- return a.getRating() > 9;
- }
- });
- // Using Lambdas
- printAnime(anime, (a) -> a.getRating() > 9);
- printAnime(anime, (a) -> a.getDesc().equals("Trash"));
- genericCondition(anime, (a) -> a.getGenre().equals("Horror"));
- predicateFunction(anime, (a) -> a.getName().charAt(0) == 'O');
- predicateFunction(anime, (a) -> a.getRating() > 7);
- getRatedAnime(anime, (a) -> a.getRating() > 7);
- //useful function: Function(T, R)
- Function<Anime, Integer> animeRating = (a) -> a.getRating();
- int hasRatingOf = animeRating.apply(anime.get(0));
- System.out.println(hasRatingOf);
- }
- public static void printAnime(List<Anime> AnimeList, animeCondition condition) {
- for (Anime a : AnimeList) {
- if (condition.test(a)) {
- a.printAnime();
- }
- }
- }
- public static void genericCondition(List<Anime> AnimeList, Condition<Anime> condition) {
- for (Anime a : AnimeList) {
- if (condition.check(a)) {
- a.printAnime();
- }
- }
- }
- public static void predicateFunction(List<Anime> AnimeList, Predicate<Anime> p) {
- for (Anime a : AnimeList) {
- if (p.test(a)) {
- a.printAnime();
- //Can I efficiently override this to print the getName method? Needs looking into.
- }
- }
- }
- //Not efficient
- public static void getRatedAnime(List<Anime> AnimeList, Predicate<Anime> p) {
- for(Anime a : AnimeList) {
- if(p.test(a)) {
- System.out.println(a.getName());
- }
- }
- }
- public static void lowestAnimeRating(List<Anime> a, int i) {
- for (Anime object : a) {
- if (object.getRating() > i) {
- object.printAnime();
- }
- }
- }
- public static void animeGenre(List<Anime> a, String g) {
- for (Anime object : a) {
- if (object.getGenre().equals(g)) {
- object.printAnime();
- }
- }
- }
- }
- @FunctionalInterface
- interface animeCondition {
- public boolean test(Anime a);
- }
- //Custom predicate interface
- @FunctionalInterface
- interface Condition<T> {
- public boolean check(T t);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement