jdk1.8新特性

  • 接口允许有default方法和static 方法

    1
    2
    3
    4
    5
    6
    7
    8
    public interface Interface8 {
    default void hello(){
    System.out.println("hello this is interface default method");
    }
    static void hi(){
    System.out.println("hi this is interface static method");
    }
    }
  • Lambda表达式

    • Lambda 是一个匿名函数,主要用来简化函数式接口(仅包含一个public方法的接口)的创建 。
    • 语法:
      • -> 为lambda操作符。具体写法为
        1
        Lambda 表达式的参数列表 -> Lambda 表达式中所需执行的功能
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        public static void main(String[] args) {
        Thread thread = new Thread(
        () -> {
        // runnable 接口 run 方法的方法体,使用lambda,则成为lambda体
        //lambda 中需要执行的代码,如果只有一行可以省略{}
        System.out.println("hello ");
        System.out.println("lambda");
        });
        thread.start();
        }
        }
        条件 表达式写法
        无参数无返回值 () -> System.out.println(“Hello WOrld”)
        有一个参数无返回值 (x) -> System.out.println(x)
        有且只有一个参数无返回值 x -> System.out.println(x)
        有多个参数,有返回值,有多条lambda体语句 (x,y) -> {System.out.println(“xxx”);return xxxx;};
        有多个参数,有返回值,只有一条lambda体语句 (x,y) -> xxxx
  • 函数式接口

    • 简单来说就是只定义了一个抽象方法的接口(Object类的public方法除外),就是函数式接口,并且还提供了注解:@FunctionalInterface
    • 函数式接口的提出是为了给Lambda表达式的使用提供更好的支持
  • Stream流

    • java.util.Stream 表示能应用在一组元素上一次执行的操作序列。Stream 操作分为中间操作或者最终操作两种,最终操作返回一特定类型的计算结果,而中间操作返回Stream本身,这样你就可以将多个操作依次串起来。Stream 的创建需要指定一个数据源,比如 java.util.Collection 的子类,List 或者 Set, Map 不支持。
    • Stream操作的三个步骤
      • 创建stream
        1
        2
        3
        4
        5
        6
        7
        8
        9
        // 1,校验通过Collection 系列集合提供的stream()或者paralleStream()
        List<String> list = new ArrayList<>();
        Strean<String> stream1 = list.stream();
        // 2.通过Arrays的静态方法stream()获取数组流
        String[] str = new String[10];
        Stream<String> stream2 = Arrays.stream(str);
        // 3.通过Stream类中的静态方法of
        Stream<String> stream3 = Stream.of("aa","bb","cc");

      • 中间操作(过滤、map)
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
        32
              /**
        * 筛选 过滤 去重
        */
        emps.stream()
        .filter(e -> e.getAge() > 10)
        .limit(4)
        .skip(4)
        // 需要流中的元素重写hashCode和equals方法
        .distinct()
        .forEach(System.out::println);


        /**
        * 生成新的流 通过map映射
        */
        emps.stream()
        .map((e) -> e.getAge())
        .forEach(System.out::println);


        /**
        * 自然排序 定制排序
        */
        emps.stream()
        .sorted((e1 ,e2) -> {
        if (e1.getAge().equals(e2.getAge())){
        return e1.getName().compareTo(e2.getName());
        } else{
        return e1.getAge().compareTo(e2.getAge());
        }
        })
        .forEach(System.out::println);
      • 终止操作
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
        32
        33
        34
        35
        36
        37
        38
        39
        40
        41
        42
        43
        44
        45
        46
        47
        48
         /**
        * 查找和匹配
        * allMatch-检查是否匹配所有元素
        * anyMatch-检查是否至少匹配一个元素
        * noneMatch-检查是否没有匹配所有元素
        * findFirst-返回第一个元素
        * findAny-返回当前流中的任意元素
        * count-返回流中元素的总个数
        * max-返回流中最大值
        * min-返回流中最小值
        */

        /**
        * 检查是否匹配元素
        */
        boolean b1 = emps.stream()
        .allMatch((e) -> e.getStatus().equals(Employee.Status.BUSY));
        System.out.println(b1);

        boolean b2 = emps.stream()
        .anyMatch((e) -> e.getStatus().equals(Employee.Status.BUSY));
        System.out.println(b2);

        boolean b3 = emps.stream()
        .noneMatch((e) -> e.getStatus().equals(Employee.Status.BUSY));
        System.out.println(b3);

        Optional<Employee> opt = emps.stream()
        .findFirst();
        System.out.println(opt.get());

        // 并行流
        Optional<Employee> opt2 = emps.parallelStream()
        .findAny();
        System.out.println(opt2.get());

        long count = emps.stream()
        .count();
        System.out.println(count);

        Optional<Employee> max = emps.stream()
        .max((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
        System.out.println(max.get());

        Optional<Employee> min = emps.stream()
        .min((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
        System.out.println(min.get());

    • 新的日期API
      • 新的日期API都是线程安全的。
      • LocalDate 本地日期
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        public static void localDateTest() {

        //获取当前日期,只含年月日 固定格式 yyyy-MM-dd 2018-05-04
        LocalDate today = LocalDate.now();
        LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS);
        System.out.println("明天的日期: "+tomorrow);
        LocalDate yesterday = tomorrow.minusDays(2);
        System.out.println("昨天的日期: "+yesterday);
        LocalDate independenceDay = LocalDate.of(2019, Month.MARCH, 12);
        DayOfWeek dayOfWeek = independenceDay.getDayOfWeek();
        System.out.println("今天是周几:"+dayOfWeek);//TUESDAY

        // 根据年月日取日期,5月就是5,
        LocalDate oldDate = LocalDate.of(2018, 5, 1);

        // 根据字符串取:默认格式yyyy-MM-dd,02不能写成2
        LocalDate yesteday = LocalDate.parse("2018-05-03");

        // 如果不是闰年 传入29号也会报错
        LocalDate.parse("2018-02-29");


        }
      • LocalTime 本地时间:
        LocalTime 定义了一个没有时区信息的时间
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
         // 当前时间
        LocalTime now = LocalTime.now();
        System.out.println(now);

        // 22:33
        LocalTime localTime = LocalTime.of(22, 33);
        System.out.println(localTime);

        // 一天中的4503秒
        LocalTime ofDay = LocalTime.ofSecondOfDay(4503);
      • LocalDateTime
        是LocalDate和LocalTime的组合体,表示的是不带时区的日期及时间
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        // 当前时间
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);

        // 当前时间加上25小时3分钟
        LocalDateTime plusMinutes = now.plusHours(25).plusMinutes(3);
        System.out.println(plusMinutes);

        // 转换
        LocalDateTime of = LocalDateTime.of(1993, 2, 6, 11, 23, 30);
        System.out.println(of);