java8新特性学习笔记

  1. 使用lambda表达式排序

      Collections.sort(temp, (String a, String b) -> {
                return b.compareTo(a);
            });
        
            Collections.sort(temp, (String a, String b) -> b.compareTo(a));
    
  2. Stream使用

      List<String> test = Arrays.asList("a", "a1", "a2", "b", "b1", "b2", "c", "c1", "ab");
    
            // 过滤
            test.stream().filter(s -> s.contains("a")).forEach(System.out::println);
    
            // 排序
            test.stream().sorted().forEach(System.out::println);
            // 映射
            test.stream().map(String::toUpperCase).forEach(System.out::println);
            // 匹配
            boolean a = test.stream().allMatch(s -> s.contains("a"));
            boolean b = test.stream().anyMatch(s -> s.contains("a"));
            boolean c = test.stream().noneMatch(s -> s.contains("a"));
            System.out.println(a + "," + b + "," + c);
    
            // 计数
            long res = test.stream().filter(s -> s.contains("a")).count();
            System.out.println(res);
            // 规约
            Optional<String> reduce = test.stream().reduce((a1, a2) -> a1 + "," + a2);
            reduce.ifPresent(System.out::println);
    
  3. Date

       Clock clock = Clock.systemDefaultZone();
            System.out.println(clock.millis());
            System.out.println(clock.instant());
            System.out.println(Date.from(clock.instant()));
    
            // 时区
            System.out.println(ZoneId.getAvailableZoneIds());
    
            ZoneId zone1 = ZoneId.of("Asia/Aden");
            ZoneId zone2 = ZoneId.of("Brazil/East");
            ZoneId zone3 = ZoneId.of("Asia/Yerevan");
            System.out.println(zone1.getRules());
            System.out.println(zone2.getRules());
            System.out.println(zone3.getRules());
    
            LocalTime now1 = LocalTime.now(zone1);
            LocalTime now2 = LocalTime.now(zone2);
            LocalTime now = LocalTime.now();
            System.out.println(now2 + "," + now1 + "," + now);
    
            LocalDate today = LocalDate.now();
            System.out.println("today:"+today);
            LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS);
            System.out.println(tomorrow);
    
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值