Featured image of post Java11新特性的六个实验

Java11新特性的六个实验

前言

最近将java升级到了11,因此学习了一些java11的一些新特性

一.局部变量类型判断

代码:

    @Test
    public void testVar() {
        // 测试局部变量类型判断特性
        var java11 = "test java11";
        var test = 1;
        System.out.println(java11);
        System.out.println(test);
    }

输出:

二.String类的新方法

代码:

 @Test
    public void testString() {
        String blank = "";
        String strip = " strip ";
        String lines = "\n\n" +
                "\n" +
                "\n";
        // 判断字符串是否为空白
        System.out.println(blank.isBlank());

        // 去除首尾空格
        System.out.println(strip.strip());
        // 去除开头空格
        System.out.println(strip.stripLeading());
        // 去除结尾空格
        System.out.println(strip.stripTrailing());
        // 重复
        System.out.println(strip.repeat(0));
        System.out.println(strip.repeat(1));
        System.out.println(strip.repeat(2));
        System.out.println(strip.repeat(3));
        // 统计行数
        System.out.println(lines.lines().count());
    }

输出:

三.使用of、copyOf方法创建不可变更的集合

代码:

@Test
    public void testList() {
        // 使用 of copyOf 创建不可变的集合 AbstractImmutableList ImmutableCollections.ListN<>(e1, e2, e3);
        // 通过这些方式创建出来的集合,是不能进行添加删除操作的,如果使用了就会报java.lang.UnsupportedOperationException异常
        var list = List.of("1", "2", "3", "4");
        var copy = List.copyOf(list);
        System.out.println(list == copy);
        var list1 = new ArrayList<>();
        var copy1 = List.copyOf(list1);
        System.out.println(list1 == copy1);
    }

输出:

四.Stream的新方法

代码:

@Test
    public void testStream() {
        // 为stream增加了4个新方法
        // 1.增加了单个参数的构造方法
        long count = Stream.ofNullable(null).count();
        System.out.println(count);
        // 2.takeWhile 一旦不满足了条件就停止 下面样例输出是123
        Stream.of(1, 2, 3, 4, 3, 2, 1).takeWhile(n -> n < 4).forEach(System.out::print);
        System.out.println();
        // 3.dropWhile 满足条件的时候都扔掉 下面样例输出是4321
        Stream.of(1, 2, 3, 4, 3, 2, 1).dropWhile(n -> n < 4).forEach(System.out::print);
        System.out.println();
        // 4. iterate重载,可以提供一个predicate判断条件来指定什么时候结束迭代。
    }

输出:

五.更加强大的Optional方法

代码:

@Test
    public void testOptional() {
        // 可以很方便的将一个Optional转成一个stream,这个repo需要自己实现或者换个东西进行替代
        Optional<GreyPolicy> byId = greyPolicyRepo.findById("");
        System.out.println(byId.stream().count());
        // 或者当optional为空的时候给他一个替代
        System.out.println("label:");
        byId.ifPresent(greyPolicy -> System.out.println(greyPolicy.getLabel()));
        Optional<GreyPolicy> replace = byId.or(() -> {
            GreyPolicy greyPolicy = new GreyPolicy();
            greyPolicy.setLabel("replace");
            return Optional.of(greyPolicy);
        });
        System.out.println("label:");
        replace.ifPresent(greyPolicy -> System.out.println(greyPolicy.getLabel()));

    }

输出:

六.JDK中自带的httpClient

代码:

   @Test
    public void testHttpClient() throws IOException, InterruptedException {
        // 可以在java.net中找到这个api,jdk自带的 http ClientAPI
        var request = HttpRequest.newBuilder().uri(URI.create("https://www.baidu.com")).GET().build();
        var httpClient = HttpClient.newHttpClient();
        HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
		System.out.println(response.body());
    }

输出:

Licensed under CC BY-NC-SA 4.0
Owner Rainbowly
Total [] user views [] site views [] page views
Built with Hugo
主题 StackJimmy 设计