iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Java9中对集合类扩展的of方法解析
  • 446
分享到

Java9中对集合类扩展的of方法解析

2024-04-02 19:04:59 446人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录Java9 集合类扩展of方法Java9集合类中重载多个of方法原因有如下描述Java9 集合类扩展of方法 package com.jd.collections; impo

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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: Java9中对集合类扩展的of方法解析

本文链接: https://www.lsjlt.com/news/134443.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作