流向 返回包含此流元素的数组。这是一个 终端操作 i、 例如,它可能会穿过水流产生结果或副作用。执行终端操作后,流管道被视为已消耗,不能再使用。
null
语法:
Object[] toArray()
返回值: 函数返回一个包含该流元素的数组。
例1:
// Java code for Stream toArray() import java.util.*; import java.util.stream.Stream; class GFG { // Driver code public static void main(String[] args) { // Creating a Stream of Integers Stream<Integer> stream = Stream.of( 5 , 6 , 7 , 8 , 9 , 10 ); // Using Stream toArray() Object[] arr = stream.toArray(); // Displaying the elements in array arr System.out.println(Arrays.toString(arr)); } } |
输出:
[5, 6, 7, 8, 9, 10]
例2:
// Java code for Stream toArray() import java.util.*; import java.util.stream.Stream; class GFG { // Driver code public static void main(String[] args) { // Creating a Stream of Strings Stream<String> stream = Stream.of( "Geeks" , "for" , "Geeks" , "GeeksQuiz" ); // Using Stream toArray() Object[] arr = stream.toArray(); // Displaying the elements in array arr System.out.println(Arrays.toString(arr)); } } |
输出:
[Geeks, for, Geeks, GeeksQuiz]
例3:
// Java code for Stream toArray() import java.util.*; import java.util.stream.Stream; class GFG { // Driver code public static void main(String[] args) { // Creating a Stream of Strings Stream<String> stream = Stream.of( "Geeks" , "for" , "gfg" , "GeeksQuiz" ); // Using Stream toArray() and filtering // the elements that starts with 'G' Object[] arr = stream.filter(str -> str.startsWith( "G" )) .toArray(); // Displaying the elements in array arr System.out.println(Arrays.toString(arr)); } } |
输出:
[Geeks, GeeksQuiz]
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END