Requisito previo: Transmitir en Java
Usando Arreglos.stream() :
Sintaxis:
public static <T> Stream<T> getStream(T[] arr) { return Arrays.stream(arr); } where, T represents generic type.
Ejemplo 1: Arrays.stream() para convertir una array de strings en una secuencia.
// Java code for converting string array // to stream using Arrays.stream() import java.util.*; import java.util.stream.*; class GFG { public static void main(String[] args) { // Converting String array to stream String[] arr = { "Geeks", "for", "Geeks" }; // Using Arrays.stream to convert an // array into Stream Stream<String> stm = Arrays.stream(arr); // Displaying elements in Stream stm.forEach(str -> System.out.print(str + " ")); } }
Geeks for Geeks
Ejemplo 2: Arrays.stream() para convertir una array int en flujo.
// Java code for converting string array // to stream using Arrays.stream() import java.util.*; import java.util.stream.*; class GFG { public static void main(String[] args) { // Converting int array to stream int arr[] = { 1, 2, 3, 4, 5 }; IntStream stm = Arrays.stream(arr); stm.forEach(a -> System.out.print(a + " ")); } }
1 2 3 4 5
Ejemplo 3: Arrays.stream() para convertir arrays largas y dobles en flujo.
// Java code for converting string array // to stream using Arrays.stream() import java.util.*; import java.util.stream.*; class GFG { public static void main(String[] args) { // Converting long array to stream long[] arrL = { 3L, 5L, 6L, 8L, 9L }; LongStream stmL = Arrays.stream(arrL); stmL.forEach(number -> System.out.print(number + " ")); System.out.println(); // Converting double array to stream double[] arrD = { 1, 2, 3, 4, 5 }; DoubleStream stmD = Arrays.stream(arrD); stmD.forEach(d -> System.out.print(d + " ")); } }
Producción:
3 5 6 8 9 1.0 2.0 3.0 4.0 5.0
Usando Stream.of(), IntStream.of(), LongStream.of() y DoubleStream.of() :
Sintaxis:
public static <T> Stream<T> getStream(T[] arr) { return Stream.of(arr); } where, T represents generic type. Syntax of other functions is similar
Nota: para arrays de objetos, Stream.of() usa internamente Arrays.stream().
Ejemplo 1: Arrays.stream() para convertir una array de strings en una secuencia.
// Java code for converting string array // to stream using Stream.of() import java.util.*; import java.util.stream.*; class GFG { public static void main(String[] args) { // Converting String array to stream String[] arr = { "Geeks", "for", "Geeks" }; // Using Arrays.stream to convert an // array into Stream Stream<String> stm = Stream.of(arr); // Displaying elements in Stream stm.forEach(str -> System.out.print(str + " ")); } }
Geeks for Geeks
Ejemplo 2: Arrays.stream() para convertir una array int en flujo.
// Java code for converting string array // to stream using IntStream.of() import java.util.*; import java.util.stream.*; class GFG { public static void main(String[] args) { // Converting int array to stream int arr[] = { 1, 2, 3, 4, 5 }; IntStream stm = IntStream.of(arr); stm.forEach(a -> System.out.print(a + " ")); } }
1 2 3 4 5
Ejemplo 3: Arrays.stream() para convertir arrays largas y dobles en flujo.
// Java code for converting string array // to stream using DoubleStream.of() import java.util.*; import java.util.stream.*; class GFG { public static void main(String[] args) { // Converting long array to stream long[] arrL = { 3L, 5L, 6L, 8L, 9L }; LongStream stmL = LongStream.of(arrL); stmL.forEach(number -> System.out.print(number + " ")); System.out.println(); // Converting double array to stream double[] arrD = { 1, 2, 3, 4, 5 }; DoubleStream stmD = DoubleStream.of(arrD); stmD.forEach(d -> System.out.print(d + " ")); } }
Producción:
3 5 6 8 9 1.0 2.0 3.0 4.0 5.0
Publicación traducida automáticamente
Artículo escrito por Sahil_Bansall y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA