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()。以下是上述两种方法之间的一些区别:
- 不同的返回类型:
对于基本数组(如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 + " ")); } }
- 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 + " ")); } }
- 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 + " ")); } }
- 对于Arrays.stream()方法:
相关用法
注:本文由纯净天空筛选整理自RishabhPrabhu大神的英文原创作品 Difference between Stream.of() and Arrays.stream() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。