Java Stream toArray()的例子

Java Stream toArray()的例子

Stream toArray() 返回一个包含该流元素的数组。这是一个 终端操作 ,也就是说,它可以遍历流,产生一个结果或副作用。在执行终端操作后,流管道被认为已被消耗,不能再被使用。

语法:

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]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程