Java 中 Stream 的中間操作
在 Java 中,流允許我們對指定元素執行函式式操作。它簡單地將源元素(例如陣列、檔案和集合框架類)透過各種內建方法傳遞,以返回結果。這些內建方法可以是中間操作或終端操作。在本文中,我們將探討 Stream 的一些中間操作方法,例如 map、filter、reduce 和 collect。這些方法可以幫助我們操作和處理資料。
Java 流的中間操作
Java 流的方法統稱為高階函式,進一步分為:
中間操作 - 它們處理輸入流的元素。
終端操作 - 它們觸發中間操作以產生非流結果,例如列印、計數和儲存。
在本節中,我們將僅透過各種示例討論流的中間操作。
Java 中流的中間操作列表:
filter()
map()
peek()
limit()
skip()
sorted()
distinct()
讓我們逐一討論它們。
filter() 方法
Java filter() 方法允許我們根據指定的條件篩選流的元素。它用於對流項應用某種行為。此方法將謂詞作為引數,並返回與謂詞匹配的元素列表。
語法
filter(predicate);
示例
以下示例說明了 filter() 方法的使用。
使用 Arrays.asList() 方法建立一個列表以儲存固定大小的列表。
現在,使用 filter() 方法以及 stream() 和 forEach() 僅篩選出奇數。這裡,stream() 指定以流的形式輸入,我們將使用終端方法 forEach() 來迭代和列印奇數。
import java.util.*; public class Fltr { public static void main(String[] args) { // creating a list of numbers List<Integer> numbers = Arrays.asList(5, 21, 32, 14, 63, 19, 10); // printing all numbers in the list System.out.println("List of all numbers: " + numbers); System.out.println("List of odd numbers: "); // filtering only odd numbers from list numbers.stream() .filter(nums -> nums % 2 == 1) .forEach(System.out::println); } }
輸出
List of all numbers: [5, 21, 32, 14, 63, 19, 10] List of odd numbers: 5 21 63 19
map() 方法
此方法將 Function 作為引數,並將其應用於 Stream 的每個元素,這將產生一個新的對映元素的 Stream。例如,我們可以使用 map() 將字串 Stream 轉換為整數 Stream。
示例
在此示例中,我們將使用 map() 方法將一個列表的元素複製到另一個列表。
import java.util.*; import java.util.stream.Collectors; public class MapExample { public static void main(String[] args) { // creating a list of integers List<Integer> AList1 = Arrays.asList(11, 22, 33, 44, 55); // to copy elements of first list to new list List<Integer> AList2 = AList1.stream() // copying the elements .map(i -> i) // collecting the result .collect(Collectors.toList()); // Printing the result System.out.println("The elements of the list: " + AList2); } }
輸出
The elements of the list: [11, 22, 33, 44, 55]
peek() 方法
我們可以使用 peek() 方法列印每個中間操作的結果。
示例
在以下示例中,我們將使用 filter() 方法以及 peek() 列印數字列表中的奇數。
import java.util.*; import java.util.stream.Collectors; public class PeekExample { public static void main(String[] args) { // creating a list of numbers List<Integer> numbers = Arrays.asList(5, 21, 32, 14, 63, 19, 10); // printing all numbers in the list System.out.println("List of all numbers: " + numbers); System.out.println("List of odd numbers: "); // filtering only odd numbers from list numbers.stream() .filter(nums -> nums % 2 == 1) // using peek to get the result .peek(nums -> System.out.println(nums)) // collecting the result .collect(Collectors.toList()); } }
輸出
List of all numbers: [5, 21, 32, 14, 63, 19, 10] List of odd numbers: 5 21 63 19
limit() 方法
使用 limit() 方法的目的是將輸出的大小限制為指定的限制。
示例
在此示例中,我們將使用 limit() 方法將中間操作的結果限制為指定的大小。
import java.util.*; public class LimitExample { public static void main(String[] args) { // creating a list of numbers List<Integer> numbers = Arrays.asList(5, 21, 32, 14, 63, 19, 10); // printing all numbers in the list System.out.println("List of all numbers: " + numbers); System.out.println("List of odd numbers: "); // filtering only odd numbers from list numbers.stream() .filter(nums -> nums % 2 == 1) // limiting the result to 2 only .limit(2) .forEach(System.out::println); } }
輸出
List of all numbers: [5, 21, 32, 14, 63, 19, 10] List of odd numbers: 5 21
skip() 方法
我們可以使用 skip() 方法丟棄結果中的前 n 個元素。它將一個整數作為引數,該引數指定將被丟棄的元素。
示例
以下示例演示瞭如何使用 skip() 方法丟棄篩選結果中的前兩個元素。
import java.util.*; public class SkipExample { public static void main(String[] args) { // creating a list of numbers List<Integer> numbers = Arrays.asList(5, 21, 32, 14, 63, 19, 10); // printing all numbers in the list System.out.println("List of all numbers: " + numbers); System.out.println("List of odd numbers: "); // filtering only odd numbers from list numbers.stream() .filter(nums -> nums % 2 == 1) // skipping first 2 elements from the result .skip(2) .forEach(System.out::println); } }
輸出
List of all numbers: [5, 21, 32, 14, 63, 19, 10] List of odd numbers: 63 19
sorted() 方法
此方法可用於按升序對流的元素進行排序。
示例
以下示例說明了如何使用 sorted() 方法按升序對流的元素進行排序。
import java.util.*; public class SortedExample { public static void main(String[] args) { // creating a list of numbers List<Integer> numbers = Arrays.asList(5, 21, 32, 14, 63, 19, 10); // printing all numbers from the list in ascending order System.out.println("List of numbers: "); // Sorting the elements of list numbers.stream() .sorted() .forEach(System.out::println); } }
輸出
List of numbers: 5 10 14 19 21 32 63
distinct() 方法
要刪除流元素中的重複項,我們可以使用 distinct() 方法。
示例
在此示例中,我們將建立一個包含重複元素的流,並應用 distinct() 方法僅列印唯一元素。
import java.util.*; public class DistinctExample { public static void main(String[] args) { // creating a list of numbers List<Integer> numbers = Arrays.asList(21, 21, 32, 14, 19, 19, 10, 10); System.out.println("Distinct list of numbers: "); // removing duplicate numbers from the list numbers.stream() .distinct() .forEach(System.out::println); } }
輸出
Distinct list of numbers: 21 32 14 19 10
結論
我們從定義流及其作為 Java 中高階函式一部分的中間操作開始本文。在下一節中,我們藉助示例程式詳細討論了中間操作方法。