zoukankan      html  css  js  c++  java
  • jdk8 函数式编程概念

    yls
    2019/11/7

    函数式接口

    1. 如果一个接口只有一个抽象方法,那么该接口就是函数式接口
    2. 如果我们在某接口上声明了FunctionalInterface注解,那么编译器就会按照函数式接口的定义来要求改接口
    3. 如果一个接口只有一个抽象方法,但并没有给该接口声明FunctionalInterface注解,那么编译器依然会将该接口看作是函数式接口,
      也就是说,FunctionalInterface注解只有检验该接口是否为函数式接口的作用,并没有决定性作用

    lambda表达式

    1. 依赖于函数式接口
    2. 在java中,lambda表达式是对象
    3. 与面向对象的不同
      1. 面向对象提前定义好行为,lambda在使用时再定义行为
            public class Test3 {
                public static void main(String[] args) {
            
                    //lambda表达式是对象,在使用时定义行为,增加使用者的灵活性
                    Function<Integer, Integer> f = v -> v * v;
                    System.out.println(Test3.compute(3, f));
            
                    System.out.println(Test3.method1(4));
                }
            
                public static int compute(int a, Function<Integer, Integer> function) {
                    return function.apply(a);
                }
            
                /**
                 * 传统写法:提前定义好行为
                 *
                 * @param a
                 * @return
                 */
                public static int method1(int a) {
                    return a * a;
                }
            }
         
        

    stream(不会改变底部数据)

    1. 一个或多个中间操作
    2. 终止操作(只有发生终止操作时,前面的中间操作才会执行)
      public static void main(String[] args) {
              List<Integer> list = Arrays.asList(1, 2, 3, 4);
              //对list集合中的数平方再求和
              //方法一
              System.out.println(list.parallelStream().map(i -> i * i).reduce(0, Integer::sum));
              //方法二
              System.out.println(list.stream().map(i -> i * i).reduce(0, (a, b) -> a + b));
      
      
              //通过stream去重
              Stream<Integer> stream=Stream.of(1,1,2,3,4);
              stream.distinct().forEach(System.out::println);
          }
      
  • 相关阅读:
    ZOJ-2008-Invitation Cards(dijkstra)
    codeforce-191E-Thwarting Demonstrations(树状数组+二分+离散)
    hdu-4612-Warm up(边双连通分量--有重边)
    TypeError: only integer scalar arrays can be converted to a scalar index
    电脑开机黑屏解决方法
    python中list与数组
    cv2.line()函数
    python中的浅拷贝与深拷贝——copy()
    pycharm中使用cv2模块
    numpy.where用法
  • 原文地址:https://www.cnblogs.com/yloved/p/11810508.html
Copyright © 2011-2022 走看看