Java8 教程第十四章之Stream Map to List

本套JAVA8教程由于是有英文翻譯過(guò)來(lái)的,如果有翻譯不對(duì)的地方還請(qǐng)多多包涵。

本節(jié)課先簡(jiǎn)單的介紹下Java8有哪些新特性,對(duì)于Java6/7版本做出哪些更改.那廢話不多說(shuō),趕緊開始今天的課程吧.

本節(jié)課內(nèi)容其實(shí)在前面已經(jīng)提到過(guò)了部分函數(shù),如collect(Collectors.toList())

那就簡(jiǎn)單的看下代碼吧

Map to List

public static void main(String[] args) {

        Map<Integer, String> map = new HashMap<>();
        map.put(10, "apple");
        map.put(20, "orange");
        map.put(30, "banana");
        map.put(40, "watermelon");
        map.put(50, "dragonfruit");

        System.out.println("\n1. Export Map Key to List...");
        List<Integer> result = map.entrySet().stream()
                .map(x -> x.getKey())
                .collect(Collectors.toList());
        result.forEach(System.out::println);


        System.out.println("\n2. Export Map Value to List...");
        List<String> result2 = map.entrySet().stream()
                .map(x -> x.getValue())
                .collect(Collectors.toList());
        result2.forEach(System.out::println);

        System.out.println("\n1. Export Map Key to List...");
        List<Integer> result = new ArrayList<>(map.keySet());
        result.forEach(System.out::println);


        System.out.println("\n2. Export Map Value to List...");
        List<String> result2 = new ArrayList<>(map.values());
        result2.forEach(System.out::println);
    }

其實(shí)上述可以不使用java8照樣實(shí)現(xiàn),但還是稍微重溫一下java8的寫法

歡迎小伙伴瀏覽哦

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容