{re...">
美文网首页java8的世界Android 开发技术分享程序员
java8 in action:第三章学习,Lambda表达式

java8 in action:第三章学习,Lambda表达式

作者: 墙角的牵牛花 | 来源:发表于2017-06-19 19:45 被阅读50次

    先判断几个Lambda的是否有效:
    1). () -> {}
    2). () -> "red"
    3). () -> {return "red";}
    4). (Integer i) -> return "me"+i;
    5). (String s) -> {"red";}

    对于1无参数返回void。
    对于2无参数返回String。
    对于3无参数返回String。
    对于4,由于return是控制流,需要加{}才能使Lambda生效。{return "me"+i;}
    对于5,"red"是表达式,不是一个语句。要么去掉{},要么改成{return "red";}

    函数式接口:只定义一个抽象方法的接口。
    例如:

    public interface Runnable{
        void run();
      }
    

    Lambda只有在需要函数式接口的时候才可以传递Lambda。关于这一点,笔者也不是特别清楚,也不好写出来误导大家。
    关于Lambda表达式,例如:

    () - > void 表示参数列表为空。
    (Apple ,Apple) -> int 表示接受两个Apple 作为参数返回int。
    try catch简写:
    private static String processFile() throws IOException {
        try(BufferedReader bufferedReader=new BufferedReader(new FileReader("/data.txt"))){
            return bufferedReader.readLine();
        }
    }
    

    这里扩展上面的try -catch简写的Lambda表达式。
    先写接口:

    public interface BufferReaderProcessor{
        String process(BufferedReader b) throws IOException;
    }
    
    private static void process(Runnable r) {
        r.run();
    }
    

    接口作为新的方法的参数并执行行为:

     private static String processFile(BufferReaderProcessor p) throws IOException {
        try(BufferedReader bufferedReader=new BufferedReader(new FileReader("F:/Java world/data.txt"))){
            return p.process(bufferedReader);//执行行为
        }
    }
    

    传递Lambda表达式。

    //打印一次读取两行
        String readTwo=processFile((BufferedReader br) -> br.readLine() +br.readLine());
        System.out.println(readTwo);
    

    函数式接口:

    java8的函数式接口很多,比如Comparable,Runnable,Callable,以及
    function包中引入的新的函数式接口,如Predcate,Consumer,Function,Suppleier等。

    说一下常见错误:

    Incompatible type specified for lambda expression's parameter i
    

    见下面的demo:

    public static<T> void forEach(List<T> list, Consumer<T> c){
            for (T t : list) {
                c.accept(t);
            }
        }
    }
    

    测试时:

    forEach(Arrays.asList(1,2,3,4,5), (int i) -> System.out.println(i));
    

    将int改成引用类型 Integer,编译通过。由于java类型分为原始类型和引用类型,而接口Consumer就不支持原始类型。并且源码里面也没有过多解释。注意就好。

    局部变量易错情况:

    int num=5;
        Runnable r1=() -> System.out.println("hello world!"+num);
        num=10;
        r1.run();
    

    如果num被赋值第二次(num=10),编译器马上报错:

    Local variable num defined in an enclosing scope must be final or effectively 
    

    意思是nums是final的,是不可能再次赋值,num=10写上去就会报错。删掉就好。

    新建一个对象,查看Apple类的构造方法:

    public Apple(int weight, String color){
         this.weight = weight;
         this.color = color;
     }
    

    用Lambda创建对象就显得麻烦一点:

    BiFunction<Integer, String, Apple> c1=Apple::new;
       Apple c2=c1.apply(110,"red");
       
       //或者
       BiFunction<Integer, String, Apple> c3=(weight,color) -> new Apple(weight, color);
       Apple c4=c3.apply(150, "green");
    

    比较Compare<T>的使用:
    先实现接口:

    class AppleSort implements Comparator<Apple>{
    
        @Override
        public int compare(Apple o1, Apple o2) {
            // TODO Auto-generated method stub
            return o1.getWeight().compareTo(o2.getWeight());
        }
        
    }
    

    初始化数据:

    List<Apple> inventory=Arrays.asList(new Apple(10,"red"),
                new Apple(300,"green"),
                new Apple(200,"yellow"));
    

    调用并比较:

    inventory.sort(new AppleSort());
    System.out.println(inventory);
    

    增加逆序功能:

    inventory.sort(new AppleSort().reversed());
    

    增加如果重量一样,按照颜色排序:

    inventory.sort(new AppleSort().reversed().thenComparing(Apple::getColor));
    

    使用Comparator.comparing类:

    inventory.sort(Comparator.comparing(Apple::getWeight).reversed().thenComparing(Apple::getColor));
    

    关于函数复合:注意andThen 和compose区别

    Function<Integer, Integer> f=x -> x+1;
        Function<Integer, Integer> g=x -> x*2;
        Function<Integer, Integer> h=f.andThen(g);//相当于g(f(x))
        int result=h.apply(2);
        System.out.println(result);
        
        Function<Integer, Integer> f1=x -> x+1;
        Function<Integer, Integer> g1=x -> x*2;
        Function<Integer, Integer> h1=f.compose(g);//相当于f(g(x))
        int result2=h1.apply(2);
    

    积分:
    简书的markdown居然写不出积分。算了不写了。直接从坐标轴x上计算,点(3,0),(20,0),与y=x+10构成的图形面积。

        public static double integrate(DoubleFunction<Double> f, double a, double b) {
        return (f.apply(a)+f.apply(b))*(b-a)/2.0;
    }
    

    调用:

    double m=integrate((double x) -> x+10,3,20);
    System.out.println(m);
    

    好了,今天就到这里了。下次学流了。

    相关文章

      网友评论

        本文标题:java8 in action:第三章学习,Lambda表达式

        本文链接:https://www.haomeiwen.com/subject/smqgqxtx.html