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

本套JAVA8教程由于是有英文翻译过来的,如果有翻译不对的地方还请多多包涵。

本节课先简单的介绍下Java8有哪些新特性,对于Java6/7版本做出哪些更改.那废话不多说,赶紧开始今天的课程吧.

本节课内容其实在前面已经提到过了部分函数,如collect(Collectors.toList())

那就简单的看下代码吧

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);
    }

其实上述可以不使用java8照样实现,但还是稍微重温一下java8的写法

欢迎小伙伴浏览哦

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容