Java11的新特性

package com.hhf.test;

import org.assertj.core.util.Lists;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Java11Test {
    public static void main(String[] args) {
        // 1、本地变量类型推断
        test1();
        System.out.println("<====================end test1=======================>");
        // 2、字符串加强
        test2();
        System.out.println("<====================end test2=======================>");
        // 3、集合加强  Jdk 里面为集合(List/ Set/ Map)都添加了 of 和 copyOf 方法,它们两个都用来创建不可变的集合
        test3();
        System.out.println("<====================end test3=======================>");
        // 4、Stream 加强
        test4();
        System.out.println("<====================end test4=======================>");
        // 5、InputStream加强
        test5();
        System.out.println("<====================end test5=======================>");
        // 6、HTTP Client API 这是 Java 9 开始引入的一个处理 HTTP 请求的的孵化 HTTP Client API,该 API 支持同步和异步,而在 Java 11 中已经为正式可用状态,你可以在 java.net 包中找到这个 API
        test6();
        System.out.println("<====================end test6=======================>");
    }

    private static void test6() {
        var request = HttpRequest.newBuilder().uri(URI.create("http://ic-park.net:40000")).GET().build();
        var client = HttpClient.newHttpClient();
        try {
            // 同步
            var response = client.send(request, HttpResponse.BodyHandlers.ofString());
            System.out.print(response.body());
            // 异步
            client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
                    .thenApply(HttpResponse::body)
                    .thenAccept(System.out::println);
            //client.sendAsync(request,HttpResponse.BodyHandlers.ofString()).thenApply(HttpResponse::body).thenAccept(System.out::println);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void test5() {
        var loader = ClassLoader.getSystemClassLoader();
        System.out.println(loader.getResource("application.properties").getPath());
        var is = loader.getResourceAsStream("application.properties");
        try {
            var parent = new File("F:\\tttttt");
            if(!parent.isDirectory()){
                parent.mkdirs();
            }
            var f = File.createTempFile("test110","txt",parent);
            var out = new FileOutputStream(f);
            is.transferTo(out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void test4() {
        // 增加单个参数构造方法,可为null
        long count = Stream.ofNullable(null).count();
        System.out.println(count);
        // 增加 takeWhile 和 dropWhile 方法
        // 获取满足条件的值
        List list = Stream.of(1, 3, 5, 2, 5, 1, 2).takeWhile(n -> n < 3).collect(Collectors.toList());
        // 找到满足条件的值删除
        List list1 = Stream.of(1, 3, 5, 2, 5, 1, 2).dropWhile(m -> m < 5).collect(Collectors.toList());

        list.forEach(li -> {
            System.out.println("li的值:"+li+",");
        });
        list1.forEach(li1 -> {
            System.out.print("li1的值:"+li1+".");
        });
    }

    private static void test3() {
        // 测试list of()、copyOf()方法 ,注意是不可变集合
        var list = List.of("java","c++","php");
        var copy = List.copyOf(list);
        System.out.print("测试不可变集合的复制是否相等:");
        System.out.print(list == copy);
        list.forEach(l->{
            System.out.println(l);
        });
        copy.forEach(c->{
            System.out.println(c);
        });

        // 测试可变集合的复制是否相等
        var strs = Lists.newArrayList("wo", "shi", "zhongguoren");
        var copy1 = List.copyOf(strs);
        System.out.println("测试可变集合的复制是否相等:");
        System.out.println(strs == copy1);
    }

    private static void test2() {
        String str = "    hello java11     ";
        // 判断字符串是否为空白
        System.out.println(" ".isBlank());
        // 去除首尾空格
        System.out.println(str.strip());
        // 去除尾部空格
        System.out.println(str.stripTrailing());
        // 去除首部空格
        System.out.println(str.stripLeading());
        // 复制字符串10次
        System.out.println(str.repeat(10));
        // 行数统计
        System.out.println("行数:"+str.lines().count());
    }

    private static void test1() {
        var name = "test";
        System.out.println(name);
        var num = 1;
        var result = num *100;
        System.out.println(result);
    }


}

 

你可能感兴趣的:(Java11的新特性)