1 import staticjava.util.Comparator.comparing;2
3 importjava.util.Arrays;4 importjava.util.List;5 importjava.util.function.DoubleFunction;6 importjava.util.function.Function;7 importjava.util.function.Predicate;8 importjava.util.function.Supplier;9
10 /**
11 * 在这里编写类的功能描述12 *13 *@authorshangjinyu14 * @created 2017/10/315 */
16 public classNotes {17 public static voidmain(String ... args){18 //构造一个待筛选的list
19 List inventory = Arrays.asList(new Apple(80, "green"),20 new Apple(155, "green"),21 new Apple(120, "red"));22
23 /**
24 * Lambda的语法是25 * (parameters) -> expression26 *27 * 函数式接口就是只定义一个抽象方法的接口28 *29 * Java8中的常用函数式接口30 * 函数式接口 函数描述符 原始类型特化31 * Predicae T->boolean IntPredicate,LongPredicate,DoublePredicate32 * Consumer T->void IntConsumer,LongConsumer,DoubleConsumer33 *34 * Function T->R IntFunction,35 * IntToDoubleFunction,36 * IntToLongFunction,37 * LongFunction,38 * LongToDoubleFunction,39 * LongToIntFunction,40 * DoubleFunction,41 * ToIntFunction,42 * ToDoubleFunction,43 * ToLongFunction,44 *45 * Supplier ()->T BooleanSupplier,IntSupplier, LongSupplier,46 * DoubleSupplier47 *48 * UnaryOperator T->T IntUnaryOperator,49 * LongUnaryOperator,50 * DoubleUnaryOperator51 *52 * BinaryOperator (T,T)->T IntBinaryOperator,53 * LongBinaryOperator,54 * DoubleBinaryOperator55 * BiPredicate (L,R)->boolean56 * BiConsumer (T,U)->void ObjIntConsumer,57 * ObjLongConsumer,58 * ObjDoubleConsumer59 * BiFunction (T,U)->R ToIntBiFunction,60 * ToLongBiFunction,61 * ToDoubleBiFunction62 */
63 //针对构造函数的方法引用
64 Supplier c1 = Apple::new; //= () -> new Apple();
65 System.out.println(c1.get());66 Function 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 redApple = apple -> apple.getColor().equals("red");75 Predicate redAndHeavyAppleOrGreen =redApple.or(a -> a.getWeight().equals(100));76 //and和or方法是按照在表达式中的位置,从左向右确定优先级的,即a.or(b).and(c)可以 作(a || b) && c77
78 //函数复合
79 Function f = x -> x + 1;80 Function g = x -> x * 2;81 Function h1 =f.andThen(g);82 int result1 = h1.apply(1);//result1 = 4;
83
84 Function 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 f, double a, doubleb) {93 return (f.apply(a) + f.apply(b)) * (b-a) / 2.0;94 }95
96 //一个实体类
97 public static classApple {98 private int weight = 0;99 private String color = "";100
101 public Apple(intweight, String color) {102 this.weight =weight;103 this.color =color;104 }105
106 publicApple() {107
108 }109
110 publicApple(Integer weight) {111 this.weight =weight;112 }113
114 publicInteger getWeight() {115 returnweight;116 }117
118 public voidsetWeight(Integer weight) {119 this.weight =weight;120 }121
122 publicString getColor() {123 returncolor;124 }125
126 public voidsetColor(String color) {127 this.color =color;128 }129
130 publicString toString() {131 return "Apple{" +
132 "color='" + color + '\'' +
133 ", weight=" + weight +
134 '}';135 }136 }137 }