JDK新特性里的常用方法

JDK8

  • Lambda表达式

  • 函数式接口

    • Supplier
    • Consumer
    • Function
    • Predicate
  • 方法引用

    • 对象名 ::引用成员方法
    • 类名 ::引用静态方法
    • 类名 ::引用实例方法
    • 类名 ::new引用构造器
  • Stream流

以上几个特性主要为了更好支持函数式编程

  • Optional类:为了解决返回null

  • 接口默认和静态方法

    • default method(default void inf(){})
    • static method (static void inf(){})
  • 日期和时间 API

    • 日期和时间类:LocalDate 、LocalTime、LocalDateTime
    • 日期和时间类(含时区):ZonedDate、ZonedTime、ZonedDateTime
    • 时间格式化与解析:DateTimeFormatter
    • 时间戳/时间线:Instant
    • 日期时间差类:Duration(用于LocalTime)、Period(用于LocalDate)
    • 时间校正器:TemporalAdjuster(TemporalAdjusters提供了常用TemporalAdjuster的实现)
  • 重复注解与类型注解

    • 重复注解:@Repeatable
    • 类型注解:@Target元注解新增了两种类型( TYPE_PARAMETER , TYPE_USE )

JDK9

  • 模块化

    module moduleA {
     //导出包
      exports com.test.utils;
      //导入模块
      requires modleB;
    }
    
  • 交互式编程: jshell工具

  • 多版本兼用 jar

  • 接口私有方法:为了抽取接口静态方法和私有方法的公共方法

  • 释放资源代码优化

    FileInputStream fileInputStream = new FileInputStream("F:/a.txt");
    try(fileInputStream){
    	//TODO 操作fileInputStream流 完成后自动关闭
    } catch (IOException e) {
      e.printStackTrace();
    }
    
  • 标识符优化:不允许单独使用"_"

  • String、StringBuffer、StringBuilder 底层结构的变化:内部维护一个字符数组变化为维护一个字节数组。

  • 集合工厂方法:快速创建只读集合(List.of() …)

  • Stream 流新增的方法

    • takeWhile()
    • dropWhile()
    • ofNullable(null)
  • 全新的 HTTPClinet

    import jdk.incubator.http.HttpClient;
    import jdk.incubator.http.HttpRequest;
    import jdk.incubator.http.HttpResponse;
    import java.net.URI;
    import java.net.URISyntaxException;
    
    public class HttpClientDemo {
      public static void main(String[] args) throws Exception {
        //创建HttpClient的对象
        HttpClient httpClient = HttpClient.newHttpClient();
        //创建请求的构造器
        HttpRequest.Builder builder = HttpRequest.newBuilder(new URI("http://www.baidu.com"));
        //使用请求构造器构建请求,并且设置请求的参数
        HttpRequest request = builder.header("user-agent", "hi").GET().build();
        //使用HttpClient发送请求,并且得到响应的对象
        HttpResponse response = httpClient.send(request,HttpResponse.BodyHandler.asString());
        //查看响应对象的信息
        System.out.println("响应状态码:"+ response.statusCode());
        System.out.println("响应的信息:"+response.body());
    }
    

JDK10

  • 删除javah工具:使用javac -h . 文件名.java生成.h文件

  • 局部变量类型推断:使用var定义变量

  • 集合新增方法

    • copyof():返回的是不可变的集合
  • InputStream & Reader新增方法

    • transferTo():输入字节流InputStream 和 输入字符流Reader新增了transferTo,可以将输入流中的数据转到输出流中.方便文件的复制
  • IO流大家族新增方法

    • 添加Charset参数的方法:通过Charset可以指定编码来操作文本(Charset是抽象类,用Charset.forName(“编码”),返回Charset的子类实例)
  • ByteArrayOutputStream新增方法

    • toString():将字节数组内存流中的数据按照指定的编码转成字符串

JDK11

  • 直接运行.java文件:不编译成.class文件 直接运行java文件java 文件名.java

  • Optional 新增方法

    • stream()
    • or()
    • orElseThrow()
    • ofNullable(null)
  • 字符串新增方法

    • isBlank():判断字符串是否为空白
    • strip():去除首尾空白(trim改进版 trim只能去除ascii码32以下的空白)
    • stripTrailing():去除尾部空格
    • stripLeading():去除首部空格
    • repeat(3):复制字符串
    • lines():返回Stream< String> , 自动识别换行并把每一行变成流的一个String元素

JDK12

  • switch表达式(预览)

    public static void main(String[] args) {
        Week day = Week.FRIDAY;
        int numLetters = switch (day) {
          case MONDAY, FRIDAY, SUNDAY -> 6;
          case TUESDAY -> 7;
          case THURSDAY, SATURDAY -> 8;
          case WEDNESDAY -> 9;
          default -> throw new IllegalStateException("What day is today?" + day);
       };
     }
    
  • 支持压缩数字格式化:NumberFormat 添加了对以紧凑形式格式化数字的支持。紧凑数字格式是指以简短或人类可读形式表示的数字。例如,在en_US语言环境中,1000可以格式化为“1K”,1000000可以格式化为“1M”,具体取决于指定的样式NumberFormat.Style。

    @Test
    public void testCompactNumberFormat(){
    	  var cnf = NumberFormat.getCompactNumberInstance(Locale.CHINA,NumberFormat.Style.SHORT);
    	  System.out.println(cnf.format(1_0000)); //1万
    	  System.out.println(cnf.format(1_9200)); //2万
    	  System.out.println(cnf.format(1_000_000)); //11亿
    	  System.out.println(cnf.format(1L << 30)); //1万
    	  System.out.println(cnf.format(1L << 40)); //1兆
    }
    
  • String新增方法

    • transform(Function):它提供的函数作为输入提供给特定的String实例,并返回该函数返回的输出
    • indent(int):该方法允许我们调整String实例的缩进
  • Files新增方法

    • mismatch(Path,Path):比较两个path的文件是否相同(如:Files.mismatch(Path.of(“tmp/a.txt”),Path.of(“tmp/b.txt”))
  • Collectors新增方法

    • teeing(Stream,Stream):用于聚合两个downstream的结果

JDK13

  • switch表达式(预览):

    String x = "3";
        int i = switch (x) {
          case "1":
            yield 1;
          case "2":
            yield 2;
          default:
            yield 3;
       };
    System.out.println(i);
    

    在JDK 12中引入了Switch表达式作为预览特性。JDK 13提出了第二个switch表达式预览。JEP 354修改了这个特性,它引入了yield语句,用于返回值。这意味着,switch表达式(返回值)应该使用yield, switch语句(不返回值)应该使用break。

  • 文本块(预览)

    • 原有方式
    String query = "select employee_id,last_name,salary,department_id\n" +
           "from employees\n" +
           "where department_id in (40,50,60)\n" +
           "order by department_id asc";
    
    • 现有方式(使用“”“作为文本块的开始符和结束符)
    String newQuery = """
       select employee_id,last_name,salary,department_id
       from employees
       where department_id in (40,50,60)
       order by department_id asc
    """;
    

你可能感兴趣的:(JAVA,JVM,并发,设计模式,java,jdk)