溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java9中集合類擴展of方法的示例分析

發(fā)布時間:2021-09-05 12:07:12 來源:億速云 閱讀:129 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要為大家展示了“Java9中集合類擴展of方法的示例分析”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學習一下“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(可變長參數(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ù)組分配,初始化和垃圾收集開銷。因為固定參數(shù)的重載方法,返回的是一個immutable list(不可變集合)。

以上是“Java9中集合類擴展of方法的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI