码迷,mamicode.com
首页 > 编程语言 > 详细

Java8_Chapter03

时间:2017-10-04 12:38:10      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:ever   operator   get   ati   复合   tco   ssi   getc   res   

  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 }

 

Java8_Chapter03

标签:ever   operator   get   ati   复合   tco   ssi   getc   res   

原文地址:http://www.cnblogs.com/3013218061shang/p/7624947.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!