Java8的学习使用

一:lambda表达式的使用。

 

1:只有在需要函数式接口的时候才可以传递Lambda

函数式接口就是只定义一个抽象方法的接口。

  1 package com.company;
  2 
  3 import java.util.ArrayList;
  4 import java.util.Arrays;
  5 import java.util.List;
  6 import java.util.function.BiFunction;
  7 import java.util.function.Function;
  8 import java.util.function.Predicate;
  9 import java.util.function.Supplier;
 10 
 11 public class FilteringApples {
 12     // 定义一个苹果类
 13     public static class Apple{
 14         private int weight = 0;
 15         private String color = "";
 16 
 17         //空参构造器
 18         public Apple(){
 19         }
 20         //一个参数的构造器
 21         public Apple(int weight) {
 22             this.weight = weight;
 23         }
 24         //两个参数的构造器
 25         public Apple(int weight, String color){
 26             this.weight = weight;
 27             this.color = color;
 28         }
 29         
 30         
 31         public Integer getWeight() {
 32             return weight;
 33         }
 34 
 35         public void setWeight(Integer weight) {
 36             this.weight = weight;
 37         }
 38 
 39         public String getColor() {
 40             return color;
 41         }
 42 
 43         public void setColor(String color) {
 44             this.color = color;
 45         }
 46 
 47         public String toString() {
 48             return "Apple{" +
 49                     "color='" + color + '\'' +
 50                     ", weight=" + weight +
 51                     '}';
 52         }
 53     }
 54     //测试
 55     public static void main(String ... args) {
 56 
 57         int portNumber = 1337;
 58         Runnable runnable = () -> System.out.println(portNumber);
 59         System.out.println("runable:"+runnable.toString());
 60         //利用空参构造器
 61         Supplier<Apple> supplier = Apple::new;
 62         Apple apple = supplier.get();
 63         //一个参数的构造器
 64         Function<Integer, Apple> function = Apple::new;
 65         Apple apply = function.apply(100);
 66         //两个参数的构造函数
 67         BiFunction<Integer, String, Apple> aNew = Apple::new;
 68         Apple green = aNew.apply(90, "green");
 69         System.out.println(green);
 70 
 71         List<Apple> inventory = Arrays.asList(new Apple(80,"green"),
 72                 new Apple(155, "green"),
 73                 new Apple(120, "red"));
 74 
 75         // [Apple{color='green', weight=80}, Apple{color='green', weight=155}]
 76         //查询为绿色的苹果
 77         List<Apple> greenApples = filterApples(inventory, FilteringApples::isGreenApple);
 78         System.out.println(greenApples);
 79 
 80         // [Apple{color='green', weight=155}]
 81         // 查询重量超过150的苹果
 82         List<Apple> heavyApples = filterApples(inventory, FilteringApples::isHeavyApple);
 83         System.out.println(heavyApples);
 84 
 85         // [Apple{color='green', weight=80}, Apple{color='green', weight=155}]
 86         // Java8 Lambda表达式  查询颜色为绿色的苹果。
 87         List<Apple> greenApples2 = filterApples(inventory, (Apple a) -> "green".equals(a.getColor()));
 88         System.out.println(greenApples2);
 89 
 90         // [Apple{color='green', weight=155}]
 91         // Java8 Lambda表达式  查询重量超过150的苹果
 92         List<Apple> heavyApples2 = filterApples(inventory, (Apple a) -> a.getWeight() > 150);
 93         System.out.println(heavyApples2);
 94 
 95         // [Apple{color='green', weight=155}]
 96         //查询苹果重量大于150,颜色也为绿色的。
 97         List<Apple> weirdApples = filterApples(inventory, (Apple a) -> a.getWeight() >150 &&
 98                 "green".equals(a.getColor()));
 99         System.out.println(weirdApples);
100     }
101 
102     //以前的需求写法:查询集合中为 green的苹果
103     public static List<Apple> filterGreenApples(List<Apple> inventory){
104         List<Apple> result = new ArrayList<>();
105         for (Apple apple: inventory){
106             if ("green".equals(apple.getColor())) {
107                 result.add(apple);
108             }
109         }
110         return result;
111     }
112     //以前的需求写法:查询重量超过150g的苹果
113     public static List<Apple> filterHeavyApples(List<Apple> inventory){
114         List<Apple> result = new ArrayList<>();
115         for (Apple apple: inventory){
116             if (apple.getWeight() > 150) {
117                 result.add(apple);
118             }
119         }
120         return result;
121     }
122 
123     //对上面的进行代码修改。上面的需求代码基本一样。把重复使用的给优化一下。
124     public static boolean isGreenApple(Apple apple) {
125         return "green".equals(apple.getColor());
126     }
127 
128     public static boolean isHeavyApple(Apple apple) {
129         return apple.getWeight() > 150;
130     }
131     //Predicate 这里使用了策略模式。定义一族算法,把它们封装起来(称为“策略”),然后在运行时选择一个算法
132     public static List<Apple> filterApples(List<Apple> inventory, Predicate<Apple> p){
133         List<Apple> result = new ArrayList<>();
134         for(Apple apple : inventory){
135             if(p.test(apple)){
136                 result.add(apple);
137             }
138         }
139         return result;
140     }
141 
142 }

 

posted @ 2020-02-13 14:22  陆伟  阅读(218)  评论(0编辑  收藏  举报