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方法:

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(不可变集合)。

来源:https://blog.csdn.net/dalinsi/article/details/78074470

标签:Java9,集合类,of
0
投稿

猜你喜欢

  • Java将Word文件转为OFD文件

    2023-05-24 01:46:35
  • Java多线程下载网图的完整案例

    2022-10-17 23:59:02
  • Android中自定义标题栏样式的两种方法

    2022-05-03 09:06:01
  • SpringBoot整合ShardingSphere的示例代码

    2022-08-17 22:52:28
  • Spring MVC返回的json去除根节点名称的方法

    2023-07-15 17:46:55
  • Spring Boot教程之提高开发效率必备工具lombok

    2021-08-23 11:12:43
  • C# 向Word中设置/更改文本方向的方法(两种)

    2023-01-12 21:37:33
  • spring cloud将spring boot服务注册到Eureka Server上的方法

    2023-12-08 19:42:09
  • SpringBoot动态更新yml文件

    2022-02-23 00:34:23
  • java8中:: 用法示例(JDK8双冒号用法)

    2023-11-25 06:21:21
  • Java启用Azure Linux虚拟机诊断设置

    2022-06-28 05:42:51
  • dotnet如何将文件删除到回收站

    2023-04-23 13:52:09
  • Java常用工具类汇总 附示例代码

    2022-07-21 17:54:15
  • visual studio 2019安装配置可编写c/c++语言的IDE环境

    2023-10-04 02:01:02
  • Java进阶:Struts多模块的技巧

    2023-06-18 09:40:47
  • Java如何在PDF中添加ToolTip工具提示

    2021-12-31 12:12:53
  • 关于BufferedReader读取文件指定字符集问题

    2023-09-03 11:10:28
  • Android利用Intent.ACTION_SEND进行分享

    2023-07-10 05:02:18
  • C#操作注册表之RegistryKey类

    2022-12-11 06:12:53
  • JAVA使用commos-fileupload实现文件上传与下载实例解析

    2022-07-08 03:47:13
  • asp之家 软件编程 m.aspxhome.com