Java9中对集合类扩展的of方法解析
作者:英杰王 发布时间:2022-06-10 09:49:43
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方法:
那这里有个问题是为什么有了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(不可变集合)。
来源:https://blog.csdn.net/dalinsi/article/details/78074470


猜你喜欢
- 目录1. 效果图2. 思路3. 实现步骤3.1 数据Bean类3.2 创建适配器3.3 继承Filterable接口3.4 过滤调用4. 优
- 1. 只有public的property能显示出来,可以通过BrowsableAttribute来控制是否显示,通过CategoryAttr
- springboot 中各种配置项纪录1. @Value最早获取配置文件中的配置的时候,使用的就是这个注解,SpEL表达式语言。// 使用起
- java调用python的几种用法如下:在java类中直接执行python语句在java类中直接调用本地python脚本使用Runtime.
- 业务现象代码中有一部分代码多次嵌套循环和数据处理,执行速度很慢解决方案通过多线程1、启用多线程private final static Ex
- Criteria的and和or进行联合查询DemoExample example=new DemoExample ();DemoExampl
- 本文实例讲述了java在网页上面抓取邮件地址的方法。分享给大家供大家参考。具体实现方法如下:import java.io.BufferedR
- 解决My eclipse 工程发布时端口占用问题如果运行后如图的错,需要进行如下操作来解决:a:打开cmd,输入netstat -ano 找
- 前言本篇文章主要讲述的是SpringBoot整合Mybatis、Druid和PageHelper 并实现多数据源和分页。其中SpringBo
- 自己整理了 spring boot 结合 Redis 的工具类引入依赖<dependency> <groupI
- 本文实例为大家分享了Android自定义双向滑动控件的具体代码,供大家参考,具体内容如下先看一下效果图1.SeekBarPressure工具
- 简评:作为一位 Android 开发者,Android Studio 肯定是每天都要打交道的,熟练掌握其中的快捷键等技巧可以提高我们不少的效
- 语法糖指计算机语言中添加的某种语法,这种语法对语言的功能并没有影响,但是更方便程序员使用。通常来说使用语法糖能够增加程序的可读性,从而减少程
- 前言我们在很大的项目开发,会发现项目引用的 dll 会很多,我想要按照不同的功能,将不同的 dll 放在不同的文件夹简单的方法是通过修改 A
- 目录一、 * 简介二、 * 的多种实现1. 基于JDK的实现2. 基于cglib的实现三、为什么要有基于cglib的实现四、两种方式的适
- 集合、数组都是对多个数据进行存储操作(主要是内存层面存储)的结构,简称Java容器。数组的特点1.数组初始化以后,长度确定不可变2.数组定义
- 关于SQLiteSQLite是一款轻型的嵌入式的遵守ACID的关系型数据库管理系统,诞生已有15个年头了。随着移动互联的发展,现在得到了更广
- 本文实例为大家分享了java数独游戏的具体代码,供大家参考,具体内容如下自己写的数独游戏,共9关,代码如下:1、DoShudu类用于产生数独
- 1.概览该教程中,我将向你展示:如何在测试时设置spring boot 日志级别。虽然我们可以在测试通过时忽略日志,但是如果需要诊断失败的测
- 1.下载JRebel and XRebel for Intellij插件2. 激活请查看这个文章http://www.cicoding.cn