本套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的寫法
歡迎小伙伴瀏覽哦