1 import static java.util.Comparator.comparing;
2
3 import java.util.Arrays;
4 import java.util.List;
5 import java.util.function.DoubleFunction;
6 import java.util.function.Function;
7 import java.util.function.Predicate;
8 import java.util.function.Supplier;
9
10 /**
11 * 在这里编写类的功能描述
12 *
13 * @author shangjinyu
14 * @created 2017/10/3
15 */
16 public class Notes {
17 public static void main(String ... args){
18 //构造一个待筛选的list
19 List<Apple> inventory = Arrays.asList(new Apple(80, "green"),
20 new Apple(155, "green"),
21 new Apple(120, "red"));
22
23 /**
24 * Lambda的语法是
25 * (parameters) -> expression
26 *
27 * 函数式接口就是只定义一个抽象方法的接口
28 *
29 * Java8中的常用函数式接口
30 * 函数式接口 函数描述符 原始类型特化
31 * Predicae<T> T->boolean IntPredicate,LongPredicate,DoublePredicate
32 * Consumer<T> T->void IntConsumer,LongConsumer,DoubleConsumer
33 *
34 * Function<T,R> T->R IntFunction<R>,
35 * IntToDoubleFunction,
36 * IntToLongFunction,
37 * LongFunction<R>,
38 * LongToDoubleFunction,
39 * LongToIntFunction,
40 * DoubleFunction<R>,
41 * ToIntFunction<T>,
42 * ToDoubleFunction<T>,
43 * ToLongFunction<T>,
44 *
45 * Supplier<T> ()->T BooleanSupplier,IntSupplier, LongSupplier,
46 * DoubleSupplier
47 *
48 * UnaryOperator<T> T->T IntUnaryOperator,
49 * LongUnaryOperator,
50 * DoubleUnaryOperator
51 *
52 * BinaryOperator<T> (T,T)->T IntBinaryOperator,
53 * LongBinaryOperator,
54 * DoubleBinaryOperator
55 * BiPredicate<L,R> (L,R)->boolean
56 * BiConsumer<T,U> (T,U)->void ObjIntConsumer<T>,
57 * ObjLongConsumer<T>,
58 * ObjDoubleConsumer<T>
59 * BiFunction<T,U,R> (T,U)->R ToIntBiFunction<T,U>,
60 * ToLongBiFunction<T,U>,
61 * ToDoubleBiFunction<T,U>
62 */
63 //针对构造函数的方法引用
64 Supplier<Apple> c1 = Apple::new; //= () -> new Apple();
65 System.out.println(c1.get());
66 Function<Integer, Apple> c2 = Apple::new;//= (weight) -> new Apple(weight);
67 System.out.println(c2.apply(10));
68
69 //复合比较器
70 inventory.sort(comparing(Apple::getWeight).reversed().thenComparing(Apple::getColor));
71 //inventory.sort((a, b) -> a.getWeight().compareTo(b.getWeight()));
72 System.out.println(inventory);
73
74 Predicate<Apple> redApple = apple -> apple.getColor().equals("red");
75 Predicate<Apple> redAndHeavyAppleOrGreen =redApple.or(a -> a.getWeight().equals(100));
76 //and和or方法是按照在表达式中的位置,从左向右确定优先级的,即a.or(b).and(c)可以 作(a || b) && c
77
78 //函数复合
79 Function<Integer, Integer> f = x -> x + 1;
80 Function<Integer, Integer> g = x -> x * 2;
81 Function<Integer, Integer> h1 = f.andThen(g);
82 int result1 = h1.apply(1);//result1 = 4;
83
84 Function<Integer, Integer> h2 = f.compose(g);
85 int result2 = h2.apply(1);//result2 = 3;
86
87 //计算 f(x) = x * 3 + 10 在x=3 到 x=7 上的积分
88 double integrate = integrate(a -> a * 3 + 10, 3 , 7);
89 System.out.println(integrate);
90 }
91
92 public static double integrate(DoubleFunction<Double> f, double a, double b) {
93 return (f.apply(a) + f.apply(b)) * (b-a) / 2.0;
94 }
95
96 //一个实体类
97 public static class Apple {
98 private int weight = 0;
99 private String color = "";
100
101 public Apple(int weight, String color) {
102 this.weight = weight;
103 this.color = color;
104 }
105
106 public Apple() {
107
108 }
109
110 public Apple(Integer weight) {
111 this.weight = weight;
112 }
113
114 public Integer getWeight() {
115 return weight;
116 }
117
118 public void setWeight(Integer weight) {
119 this.weight = weight;
120 }
121
122 public String getColor() {
123 return color;
124 }
125
126 public void setColor(String color) {
127 this.color = color;
128 }
129
130 public String toString() {
131 return "Apple{" +
132 "color='" + color + '\'' +
133 ", weight=" + weight +
134 '}';
135 }
136 }
137 }