Advertisement
Glaciation96

Practising with Lambdas

Dec 11th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.74 KB | None | 0 0
  1. package imLearning;
  2.  
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.function.*;
  6.  
  7. public class muscle {
  8.  
  9.     public static void main(String[] args) {
  10.  
  11.         List<Anime> anime = Arrays.asList(
  12.                 new Anime("One Piece", "TBE", "Shounen", 10),
  13.                 new Anime("Naruto", "Trash", "Shounen", 1),
  14.                 new Anime("DragonBall", "Average", "Shounen", 6),
  15.                 new Anime("Attack on Titan", "Good", "Fantasy", 7),
  16.                 new Anime("Sword Art Online", "Trash", "Isekai", 2),
  17.                 new Anime("One Punch Man", "Good", "Shounen", 8),
  18.                 new Anime("Tokyo Ghoul", "Average", "Horror", 5)
  19.                 );
  20.  
  21.         // Method calling
  22.         lowestAnimeRating(anime, 9);
  23.         animeGenre(anime, "Shounen");
  24.  
  25.         // Without Lambdas
  26.         printAnime(anime, new animeCondition() {
  27.             @Override
  28.             public boolean test(Anime a) {
  29.                 return a.getRating() > 9;
  30.             }
  31.         });
  32.  
  33.         // Using Lambdas
  34.         printAnime(anime, (a) -> a.getRating() > 9);
  35.         printAnime(anime, (a) -> a.getDesc().equals("Trash"));
  36.  
  37.         genericCondition(anime, (a) -> a.getGenre().equals("Horror"));
  38.  
  39.         predicateFunction(anime, (a) -> a.getName().charAt(0) == 'O');
  40.  
  41.         predicateFunction(anime, (a) -> a.getRating() > 7);
  42.        
  43.         getRatedAnime(anime, (a) -> a.getRating() > 7);
  44.        
  45.         //useful function: Function(T, R)
  46.         Function<Anime, Integer> animeRating = (a) -> a.getRating();
  47.         int hasRatingOf = animeRating.apply(anime.get(0));
  48.         System.out.println(hasRatingOf);
  49.  
  50.     }
  51.    
  52.     public static void printAnime(List<Anime> AnimeList, animeCondition condition) {
  53.         for (Anime a : AnimeList) {
  54.             if (condition.test(a)) {
  55.                 a.printAnime();
  56.             }
  57.         }
  58.     }
  59.  
  60.     public static void genericCondition(List<Anime> AnimeList, Condition<Anime> condition) {
  61.         for (Anime a : AnimeList) {
  62.             if (condition.check(a)) {
  63.                 a.printAnime();
  64.             }
  65.         }
  66.     }
  67.  
  68.     public static void predicateFunction(List<Anime> AnimeList, Predicate<Anime> p) {
  69.         for (Anime a : AnimeList) {
  70.             if (p.test(a)) {
  71.                 a.printAnime();
  72.                 //Can I efficiently override this to print the getName method? Needs looking into.
  73.             }
  74.         }
  75.     }
  76.    
  77.     //Not efficient
  78.     public static void getRatedAnime(List<Anime> AnimeList, Predicate<Anime> p) {
  79.         for(Anime a : AnimeList) {
  80.             if(p.test(a)) {
  81.                 System.out.println(a.getName());
  82.            }
  83.         }
  84.     }
  85.  
  86.     public static void lowestAnimeRating(List<Anime> a, int i) {
  87.         for (Anime object : a) {
  88.             if (object.getRating() > i) {
  89.                 object.printAnime();
  90.             }
  91.         }
  92.     }
  93.  
  94.     public static void animeGenre(List<Anime> a, String g) {
  95.         for (Anime object : a) {
  96.             if (object.getGenre().equals(g)) {
  97.                 object.printAnime();
  98.             }
  99.         }
  100.     }
  101. }
  102.  
  103. @FunctionalInterface
  104. interface animeCondition {
  105.     public boolean test(Anime a);
  106. }
  107.  
  108. //Custom predicate interface
  109. @FunctionalInterface
  110. interface Condition<T> {
  111.     public boolean check(T t);
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement