JDK8新特性(一)

2020-11-23  本文已影响0人  家hao

一、JDK8之default关键字

public interface Animal {
    void run();
    void eat();
    default void breath(){
        System.out.println("使用氧气呼吸");
    }
    static void test(){
        System.out.println("这是静态方法");
    }
}

二、JDK8之新增base64加解密API

public class test {

    @Test
    public void test() throws IOException {
        BASE64Encoder encoder = new BASE64Encoder();
        BASE64Decoder decoder = new BASE64Decoder();
        String text = "旧版编解码";
        byte[] textByte = text.getBytes("UTF-8");
        //编码
        String encodedText = encoder.encode(textByte);
        System.out.println(encodedText);
        //解码
        System.out.println(new String(decoder.decodeBuffer(encodedText),"UTF-8"));
    }
}
public class test {

    @Test
    public void test() throws IOException {
        Base64.Decoder decoder = Base64.getDecoder();
        Base64.Encoder encoder = Base64.getEncoder();
        String text = "JDK8之新编解码";
        byte[] textByte = text.getBytes("UTF-8");
        //编码
        String encodedText = encoder.encodeToString(textByte);
        System.out.println(encodedText);
        //解码
        System.out.println(new String(decoder.decode(encodedText), "UTF-8"));
    }
}

三、JDK8之时间日期处理类

import org.junit.Test;
import java.time.*;
import java.time.format.DateTimeFormatter;

public class test {

    @Test
    public void test() {
        LocalDate today = LocalDate.now();
        System.out.println(today);
        //获取年月日
        System.out.println(today.getYear());
        System.out.println(today.getMonth());
        System.out.println(today.getDayOfMonth());
        System.out.println(today.getDayOfWeek());
        //加减年份,加厚返回的对象才是修改后的,旧的依旧是旧的
        LocalDate changeDate = today.plusYears(1);
        System.out.println(changeDate.getYear());
        System.out.println(today.getYear());
        //日期比较
        //是否在之前
        System.out.println("isbefore:"+today.isBefore(changeDate));
        //是否在之后
        System.out.println("isbefore:"+today.isAfter(changeDate));

        // jdk8引入 DateTimeFormatter是线程安全的SimpleDateFormat
        LocalDateTime lt = LocalDateTime.now();
        System.out.println(lt);// 2019-11-07T23:12:29.056
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String ldtStr = dtf.format(lt);
        System.out.println(ldtStr);// 2019-11-07 23:12:29

        //获取指定时间对象
        LocalDateTime ldt = LocalDateTime.of(2020,10,1,10,40,30);
        System.out.println(changeDate);// 2020-10-01T10:40:30

        //Duration基于时间值,Period基于日期值,
        //Duration类表示秒或纳秒时间间隔
        //适合处理较短的时间,需要更高的精确性。使用between()方法比较两个瞬间的差:
        Instant start = Instant.parse("2019-11-16T11:15:30.00Z");
        Instant end = Instant.parse("2019-11-16T11:16:30.00Z");
        Duration duration = Duration.between(start,end);//第⼆个参数减第⼀个参数
        System.out.println(duration.toDays());//两个时间差的天数
        System.out.println(duration.toHours());//两个时间差的⼩时数
        System.out.println(duration.toMinutes());//两个时间差的分钟数
        System.out.println(duration.toMillis());//两个时间差的毫秒数
        System.out.println(duration.toNanos());//两个时间差的纳秒数

        //Period 类表示一段时间的年、月、日,使用between()方法获取两个日期之间的差
        LocalDate startDate = LocalDate.of(2019, 11, 16);
        LocalDate endDate = LocalDate.of(2020, 1, 1);
        Period period = Period.between(startDate,endDate);
        System.out.println("per:"+period.getYears());//两个时间差的年数
        System.out.println("per:"+period.getMonths());//两个时间差的月
    }
}

四、JDK8之Optional类

Optional<Student> opt = Optional.of(student);
Optional<Student> opt = Optional.ofNullable(student);
public class Student {
    private int age;
    public int getAge() {return age; }
    public void setAge(int age) {this.age = age;}
}
import java.util.Optional;

public class test {

    @Test
    public void test(){
        Student student = new Student();
        Optional<Student> opt = Optional.ofNullable(student);
        if(opt.isPresent()){
            System.out.println("optional不为空");
            Student s = opt.get();
        }else {
            System.out.println("optional为空");
        }
    }
@Test
public void test(){
    Student student = null;
    int result = Optional.ofNullable(student).map(obj->obj.getAge()).orElse(4);
    System.out.println(result);
}

五、JDK8之 lambda表达式

//JDK8之前创建线程
new Thread(new Runnable() {
    @Override
    public void run() {
        System.out.println("jdk8之前创建线程");
    }
});

//jdk8之后Lambda表达式则只需要使用一句话
new Thread(() -> System.out.println("jdk8之后创建线程"));
//jdk8之前
List<String> list =Arrays.asList("aaa","ggg","ffff","ccc");
Collections.sort(list, new Comparator<String>() {
    @Override
    public int compare(String a, String b) {
        return b.compareTo(a);
    }
});
for (String string : list) {
    System.out.println(string);
}

//jdk8之后
List<String> list =Arrays.asList("aaa","ggg","ffff","ccc");
Collections.sort(list, (a,b)->b.compareTo(a));
for (String string : list) {
    System.out.println(string);
}
  1. 第一部分为括号内用逗号分隔的形式参数,参数是函数式接口里面方法的参数;第二部分为一个箭头符号:->;第三部分为方法体,可以是表达式和代码块
  2. 参数列列表 :
    括号中参数列列表的数据类型可以省略不写
    括号中的参数只有一个,那么参数类型和()都可以省略不写
  3. 方法体:
    如果{}中的代码只有一行,无论有返回值,可以省略{},return,分号,要一起省略,其他则需要加上

重构现有臃肿代码,更高的开发效率,尤其是集合Collection操作的时候

上一篇下一篇

猜你喜欢

热点阅读