當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Stream.of()和Arrays.stream()的區別用法及代碼示例


Arrays.stream()

Java中Arrays類的stream(T [] array)方法用於從作為參數及其元素的參數傳遞的數組中獲取順序流。它返回一個順序的Stream,它具有作為參數傳遞的數組元素作為其源。

例:

// Java program to demonstrate Arrays.stream() method 
  
import java.util.*; 
import java.util.stream.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // Creating a String array 
        String[] arr = { "Geeks", "for", "Geeks" }; 
  
        // Using Arrays.stream() to convert 
        // array into Stream 
        Stream<String> stream = Arrays.stream(arr); 
  
        // Displaying elements in Stream 
        stream.forEach(str -> System.out.print(str + " ")); 
    } 
}

Stream.of()

Stream of(T…values)返回一個順序有序的流,其元素為指定值。 Stream.of()方法僅對非基本類型調用Arrays.stream()方法。


例:

// Java code for Stream of(T... values) 
// to get a sequential ordered stream whose 
// elements are the specified values. 
  
import java.util.*; 
import java.util.stream.Stream; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
        // Creating an Stream 
        Stream stream = Stream.of("Geeks", "for", "Geeks"); 
  
        // Displaying the sequential ordered stream 
        stream.forEach(str -> System.out.print(str + " ")); 
    } 
}

這兩種方法都是從指定數組創建順序流的兩種最常用方法。當使用非原始類型T調用時,這兩種方法均返回Stream <T>。

Difference between Arrays.stream() and Stream.of()

即使Stream.of()是Arrays.stream()方法的包裝,也存在某些差異,這些差異明確說明了何時使用Arrays.stream()或何時使用Stream.of()。以下是上述兩種方法之間的一些區別:

  1. 不同的返回類型

    對於基本數組(如int [],long []等),Arrays.stream()和Stream.of()具有不同的返回類型。

    例:傳遞一個整數數組,Stream.of()方法返回Stream而Arrays.stream()返回IntStream。

    // Java program to demonstrate return type 
    // of Arrays.stream() and Stream.of() method 
    // for primitive arrays 
      
    import java.util.*; 
    import java.util.stream.*; 
      
    class GFG { 
      
        public static void main(String[] args) 
        { 
            // Creating an integer array 
            int arr[] = { 1, 2, 3, 4, 5 }; 
      
            // --------- Using Arrays.stream() --------- 
      
            // to convert int array into Stream 
            IntStream intStream = Arrays.stream(arr); 
      
            // Displaying elements in Stream 
            intStream.forEach(str -> System.out.print(str + " ")); 
      
            // --------- Using Stream.of() --------- 
      
            // to convert int array into Stream 
            Stream<int[]> stream = Stream.of(arr); 
      
            // Displaying elements in Stream 
            stream.forEach(str -> System.out.print(str + " ")); 
        } 
    }
  2. Stream.of()需要展平,而Arrays.stream()不需要展平

    作為處理原始類型的Stream的理想類,是它們的原始Stream類型(例如IntStream,LongStream等)。因此,Stream.of()需要在使用之前顯式地展平為其原始Stream。

    例:

    // Java program to demonstrate need of flattenning 
    // Stream.of() method returned type for primitive arrays 
      
    import java.util.*; 
    import java.util.stream.*; 
      
    class GFG { 
      
        public static void main(String[] args) 
        { 
            // Creating an integer array 
            int arr[] = { 1, 2, 3, 4, 5 }; 
      
            // --------- Using Arrays.stream() --------- 
      
            // to convert int array into Stream 
            IntStream intStream = Arrays.stream(arr); 
      
            // Displaying elements in Stream 
            intStream.forEach(str -> System.out.print(str + " ")); 
      
            // --------- Using Stream.of() --------- 
      
            // to convert int array into Stream 
            Stream<int[]> stream = Stream.of(arr); 
      
            // ***** Flattening of Stream<int[]> into IntStream ***** 
      
            // flattenning Stream<int[]> into IntStream 
            // using flatMapToInt() 
            IntStream intStreamNew = stream.flatMapToInt(Arrays::stream); 
      
            // Displaying elements in IntStream 
            intStreamNew.forEach(str -> System.out.print(str + " ")); 
        } 
    }
  3. Stream.of()是通用的,而Arrays.stream不是

    Arrays.stream()方法僅適用於int [],long []和double []類型的原始數組,並分別返回IntStream,LongStream和DoubleStream。對於其他原始類型,Arrays.stream()將不起作用。
    另一方麵,Stream.of()返回類型T的通用Stream(Stream)。因此,它可以與任何類型一起使用。

    例:

    • 對於Arrays.stream()方法:
      // Java program to demonstrate return type 
      // of Arrays.stream() method 
      // for primitive arrays of char 
        
      import java.util.*; 
      import java.util.stream.*; 
        
      class GFG { 
        
          public static void main(String[] args) 
          { 
              // Creating a character array 
              char arr[] = { '1', '2', '3', '4', '5' }; 
        
              // --------- Using Arrays.stream() --------- 
              // This will throw error 
        
              // to convert char array into Stream 
              Arrays.stream(arr); 
          } 
      }

      輸出:

      Compilation Error in java code:-
      prog.java:20:error:no suitable method found for stream(char[])
      Arrays.stream(arr);
      ^

    • 對於Stream.of()方法:
      // Java program to demonstrate return type 
      // of Stream.of() method 
      // for primitive arrays of char 
        
      import java.util.*; 
      import java.util.stream.*; 
        
      class GFG { 
        
          public static void main(String[] args) 
          { 
              // Creating a character array 
              char arr[] = { '1', '2', '3', '4', '5' }; 
        
              // --------- Using Stream.of() --------- 
              // Will work efficiently 
        
              // to convert int array into Stream 
              Stream<char[]> stream = Stream.of(arr); 
        
              // Displaying elements in Stream 
              stream.forEach(str -> System.out.print(str + " ")); 
          } 
      }


相關用法


注:本文由純淨天空篩選整理自RishabhPrabhu大神的英文原創作品 Difference between Stream.of() and Arrays.stream() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。