Java9中對集合類擴(kuò)展的of方法解析
Java9 集合類擴(kuò)展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集合類中重載多個(gè)of方法原因
在java9 api的集合類中,有很多看似一樣的重載of方法:
那這里有個(gè)問題是為什么有了VarArgs(可變長參數(shù))方法,還需要定義那么多重載的方法呢?查看官方的更新日志中可以發(fā)現(xiàn)
有如下描述
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調(diào)用引起的數(shù)組分配,初始化和垃圾收集開銷。因?yàn)楣潭▍?shù)的重載方法,返回的是一個(gè)immutable list(不可變集合)。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
String轉(zhuǎn)BigDecimal,BigDecimal常用操作,以及避免踩坑記錄
這篇文章主要介紹了String轉(zhuǎn)BigDecimal,BigDecimal常用操作,以及避免踩坑記錄,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07Mybatis中一條SQL使用兩個(gè)foreach的問題及解決
這篇文章主要介紹了Mybatis中一條SQL使用兩個(gè)foreach的問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02Java網(wǎng)絡(luò)IO模型詳解(BIO、NIO、AIO)
Java支持BIO、NIO和AIO三種網(wǎng)絡(luò)IO模型,BIO是同步阻塞模型,適用于連接數(shù)較少的場景,NIO是同步非阻塞模型,適用于處理多個(gè)連接,支持自JDK1.4起,AIO是異步非阻塞模型,適用于異步操作多的場景,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-10-10基于Nacos實(shí)現(xiàn)Spring Cloud Gateway實(shí)現(xiàn)動態(tài)路由的方法
這篇文章主要介紹了基于Nacos實(shí)現(xiàn)Spring Cloud Gateway實(shí)現(xiàn)動態(tài)路由的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06