温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java9中集合类扩展of方法的示例分析

发布时间:2021-09-05 12:07:12 来源:亿速云 阅读:126 作者:小新 栏目:开发技术

这篇文章主要为大家展示了“Java9中集合类扩展of方法的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Java9中集合类扩展of方法的示例分析”这篇文章吧。

Java9 集合类扩展of方法

package com.jd.collections;
import org.junit.Test;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class StreamTest {
    @Test
    public void testSet() {
        Set<Integer> integerSet = Set.of(1, 2, 3, 4, 5, 6, 7, 8);
        System.out.println(integerSet);
    }
    @Test
    public void testList() {
        List<Integer> integerSet = List.of(1, 2, 3, 4, 5, 6, 7, 8);
        System.out.println(integerSet);
    }
    @Test
    public void testMap() {
        Map<String, String> stringMap = Map.of("k1", "v1", "k2", "v2", "k3", "v3");
        System.out.println(stringMap);
        Map.Entry<String, String> entry1 = Map.entry("k1", "v1");
        Map.Entry<String, String> entry2 = Map.entry("k11", "v11");
        Map.Entry<String, String> entry3 = Map.entry("k12", "v12");
        Map<String, String> mapOfEntries = Map.ofEntries(entry1, entry2, entry3);
        System.out.println(mapOfEntries);
    }
    @Test
    public void testStream1() {
        Optional<Integer> integerOptional = Stream.ofNullable(Integer.valueOf("1232")).findAny();
        System.out.println(integerOptional.get());
    }
    @Test
    public void testStream2() {
        Stream.of(1, 2, 3, 4, 5, 6).dropWhile(x -> x == 6)/*.takeWhile(x -> x == 2)*/.forEach(System.out::println);
    }
    @Test
    public void testStream3() {
        IntStream.of(1, 2, 3, 4, 5, 6).forEach(System.out::println);
    }
    @Test
    public void testStream4() {
        IntStream.iterate(1, i -> i < 10, i -> i + 2).forEach(System.out::println);
    }
//    @Test
//    public void testFlow() {
//        Flow.Processor
//    }
}

Java9集合类中重载多个of方法原因

在java9 api的集合类中,有很多看似一样的重载of方法:

Java9中集合类扩展of方法的示例分析

那这里有个问题是为什么有了VarArgs(可变长参数)方法,还需要定义那么多重载的方法呢?查看官方的更新日志中可以发现

有如下描述

http://openjdk.java.net/jeps/269

These will include varargs overloads, so that there is no fixed limit on the collection size. However, the collection instances so created may be tuned for smaller sizes. Special-case APIs (fixed-argument overloads) for up to ten of elements will be provided. While this introduces some clutter in the API, it avoids array allocation, initialization, and garbage collection overhead that is incurred by varargs calls. Significantly, the source code of the call site is the same regardless of whether a fixed-arg or varargs overload is called.

大致得意思是,虽然重载了这么多of方法会造成api的混乱,但它避免了varargs调用引起的数组分配,初始化和垃圾收集开销。因为固定参数的重载方法,返回的是一个immutable list(不可变集合)。

以上是“Java9中集合类扩展of方法的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI