Java8 將數據流轉換爲列表

引言

示例演示如何經過 Collectors.toList 將數據流轉換爲 List。html

  1. 這個在前面也屢次出現過
public static void main(String[] args) {

    Stream<String> language = Stream.of("java", "python", "node");

    //Convert a Stream to List
    List<String> list = language.collect(Collectors.toList());
    list.forEach(System.out::println);
  }

輸出:java

java
python
node
  1. 過濾數字 3 並將其轉換爲 List
Stream<Integer> number = Stream.of(1, 2, 3, 4, 5);
    List<Integer> collect = number.filter(x -> x != 3).collect(Collectors.toList());
    collect.forEach(System.out::println);

輸出:node

1
2
4
5
源碼見: java-8-demo

系列文章詳見:Java 8 教程python

圖片描述

相關文章
相關標籤/搜索