美文网首页
jdk 1.8 新特性使用

jdk 1.8 新特性使用

作者: 孑鼠 | 来源:发表于2021-11-11 11:50 被阅读0次

    虽然jdk1.8已经是很久以前更新的了,但是开发人员对于1.8的新特性使用可能不是很多,接下来文章会说一些1.8中用的比较多的功能,赶紧学起来

    1,首先用的最多的是stream流的使用

    以前我们对于集合的遍历是这样的(举个例子)

       for (User user : list) {
                //执行便利后的操作
                user.setAge(user.getAge()+1);
            }
    

    stream在jdk1.8中使用

     list.stream().forEach(r->{
                r.setAge(r.getAge()+1);
            });
    

    在1.8中流对于集合还有很便利的操作

    //获取集合中年龄大于10的用户
            list.stream().filter(user -> user.getAge()>10).collect(Collectors.toList());
            //根据用户的年纪排序
            list.stream().sorted(Comparator.comparing(User::getAge)).collect(Collectors.toList());
            //获取该集合中年龄最大的用户
            list.stream().max(Comparator.comparing(User::getAge)).get();
            //将用户集合中的用户年龄单独取出作为一个集合(常用与日常开发中根据主表id去查询子表数据的业务中)
            list.stream().map(User::getAge).collect(Collectors.toList());    
            //将集合按照key是用户id,value是用户本省的map集合
            list.stream().collect(Collectors.toMap(User::getId, Function.identity()));
    

    这些是开发中常用的一些流的操作,我们在开发中可以组合起来操作

    2,接下来是对于流的操作(输入流,输出流)

    众所周知,我们在之前使用输入流,输出流的时候每次使用完之后都需要关闭流,但是在1.8以后我们还需要关闭吗?不需要了,1.8已经帮我们实现了自动关闭的功能了,那编码和以前有什么区别呢,1.8又是怎么样实现的呢?
    那这其中最关键的一个接口

    public interface AutoCloseable {
        /**
         * Closes this resource, relinquishing any underlying resources.
         * This method is invoked automatically on objects managed by the
         * {@code try}-with-resources statement.
         *
         * <p>While this interface method is declared to throw {@code
         * Exception}, implementers are <em>strongly</em> encouraged to
         * declare concrete implementations of the {@code close} method to
         * throw more specific exceptions, or to throw no exception at all
         * if the close operation cannot fail.
         *
         * <p> Cases where the close operation may fail require careful
         * attention by implementers. It is strongly advised to relinquish
         * the underlying resources and to internally <em>mark</em> the
         * resource as closed, prior to throwing the exception. The {@code
         * close} method is unlikely to be invoked more than once and so
         * this ensures that the resources are released in a timely manner.
         * Furthermore it reduces problems that could arise when the resource
         * wraps, or is wrapped, by another resource.
         *
         * <p><em>Implementers of this interface are also strongly advised
         * to not have the {@code close} method throw {@link
         * InterruptedException}.</em>
         *
         * This exception interacts with a thread's interrupted status,
         * and runtime misbehavior is likely to occur if an {@code
         * InterruptedException} is {@linkplain Throwable#addSuppressed
         * suppressed}.
         *
         * More generally, if it would cause problems for an
         * exception to be suppressed, the {@code AutoCloseable.close}
         * method should not throw it.
         *
         * <p>Note that unlike the {@link java.io.Closeable#close close}
         * method of {@link java.io.Closeable}, this {@code close} method
         * is <em>not</em> required to be idempotent.  In other words,
         * calling this {@code close} method more than once may have some
         * visible side effect, unlike {@code Closeable.close} which is
         * required to have no effect if called more than once.
         *
         * However, implementers of this interface are strongly encouraged
         * to make their {@code close} methods idempotent.
         *
         * @throws Exception if this resource cannot be closed
         */
        void close() throws Exception;
    }
    

    你去看看jdk中常用的流,你去看看他们的底层代码,没错都继承/实现了这个接口,这个接口的作用就是帮我们关闭流,那我们现在代码中应该怎么写呢?

     try (FileOutputStream out = new FileOutputStream(new File("C://XXX"));
                 FileInputStream in = new FileInputStream(new File("C://XXX"));
            ) {
                in.read();
                out.write(12);
            } catch (IOException e) {
                e.printStackTrace();
            }
    

    这只是举个例子,具体的业务逻辑啥的根据自己功能来写,这两个流就会在执行完之后自动关闭,当然我们也可以模拟jdk对于流的操作,比如我们在使用redis来做分布式锁的时候我们可以给我们的分布式锁的工具类实现AutoCloseable 接口重写close方法,然后在close方法中完成对与锁的释放,根据自己业务需求可自由发挥使用

    3,Optional的使用

    Optional可以在很大程度上减少空指针的出现,话不多说直接上代码

            ArrayList<User> list = new ArrayList<>();
            //判断user是否为空如果为空就返回新创建的user
            Optional.ofNullable(user).orElse(new User());
            Optional<User> max = list.stream().max(Comparator.comparing(User::getAge));
            //判断对象是否存在
            boolean present = max.isPresent();
            //获取返回的具体的对象实体
            User user1 = max.get();
    

    4,时间类LocalDateTime及其周边

    在jdk1.8以后对于时间日历的操作更加方便了

           Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis() * 1000);
            //获取年
            calendar.get(Calendar.YEAR);
            //获取月
            calendar.get(Calendar.MONTH);
            //获取日
            calendar.get(Calendar.DAY_OF_MONTH);
            //获取时
            calendar.get(Calendar.HOUR_OF_DAY);
            //获取分
            calendar.get(Calendar.MINUTE);
            //获取秒
            calendar.get(Calendar.MILLISECOND);
    

    现在的操作

            LocalDateTime now = LocalDateTime.now();
            int year = now.getYear();
            int monthValue = now.getMonthValue();
            int dayOfMonth = now.getDayOfMonth();
            int dayOfMonth1 = now.getDayOfMonth();
            int hour1 = now.getHour();
            int minute1 = now.getMinute();
            int second = now.getSecond();
    

    更加方便的是对时间/日期的操作

            LocalDateTime now = LocalDateTime.now();
            int year = now.getYear();
            int monthValue = now.getMonthValue();
            int dayOfMonth = now.getDayOfMonth();
            int dayOfMonth1 = now.getDayOfMonth();
            int hour1 = now.getHour();
            int minute1 = now.getMinute();
            int second = now.getSecond();
            //添加一天
            LocalDateTime localDateTime = now.plusDays(1);
            //添加一个小时
            LocalDateTime localDateTime1 = now.plusHours(1);
            //添加一分钟;
            LocalDateTime localDateTime2 = now.plusMinutes(1);
            //添加一周
            LocalDateTime localDateTime3 = now.plusWeeks(1);
            //比较两个时间的大小
            int i = localDateTime.compareTo(localDateTime3);
            localDateTime1.isAfter(localDateTime2);
            localDateTime1.isBefore(localDateTime2);
            localDateTime1.isEqual(localDateTime2);
            //此外还提供了更加便捷的创建日历、时间的方式
            LocalDateTime localDateTime4 = LocalDateTime.of(2020, 10, 21, 15, 30, 22);
            LocalDate localDate = LocalDate.of(2020, 10, 21);
            LocalTime localTime = LocalTime.of(12, 20, 12, 223);
            //本月的第1天
            localDate.with(TemporalAdjusters.firstDayOfMonth());
            //下月的第1天
            localDate.with(TemporalAdjusters.firstDayOfNextMonth());
            localDate.with(TemporalAdjusters.firstDayOfNextYear());
            //日期转时间戳ZoneOffset.of("+8")设置的是时区
            Long second1 = localDateTime4.toEpochSecond(ZoneOffset.of("+8"));
            //获取毫秒数
            Long milliSecond = localDateTime4.toInstant(ZoneOffset.of("+8")).toEpochMilli();
            //转字符串
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
            String format = localDateTime4.format(formatter);
            //还有一种创建的时候带时区的日历类(方法相似就不做阐述了)
            ZoneId zoneId = ZoneId.of("UTC+1");
            ZonedDateTime zonedDateTime = ZonedDateTime.now(zoneId);
    

    在实际开发中灵活运用可以有效地提高开发的效率和代码质量。

    相关文章

      网友评论

          本文标题:jdk 1.8 新特性使用

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