IntStream en caja() en Java

IntStream boxed() devuelve un Stream que consta de los elementos de este flujo, cada uno encuadrado en un Integer.

Nota: IntStream boxed() es una operación intermedia. Estas operaciones son siempre perezosas. Las operaciones intermedias se invocan en una instancia de Stream y, una vez que finalizan su procesamiento, dan una instancia de Stream como salida.
Sintaxis:

Stream<Integer> boxed()

Parámetros:

  1. Flujo : Una secuencia de elementos que soportan operaciones agregadas secuenciales y paralelas.
  2. Entero: la clase Integer envuelve un valor del tipo primitivo int en un objeto. Un objeto de tipo Integer contiene un solo campo cuyo tipo es int.

Valor devuelto: la función devuelve un Stream encuadrado en un entero.

Ejemplo 1 :

// Java code for IntStream boxed()
import java.util.*;
import java.util.stream.Stream;
import java.util.stream.IntStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an IntStream
        IntStream stream = IntStream.range(3, 8);
  
        // Creating a Stream of Integers
        // Using IntStream boxed() to return
        // a Stream consisting of the elements
        // of this stream, each boxed to an Integer.
        Stream<Integer> stream1 = stream.boxed();
  
        // Displaying the elements
        stream1.forEach(System.out::println);
    }
}

Producción :

3
4
5
6
7

Ejemplo 2:

// Java code for IntStream boxed()
import java.util.*;
import java.util.stream.Stream;
import java.util.stream.IntStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an IntStream
        IntStream stream = IntStream.range(3, 8);
  
        // Creating a Stream of Integers
        // Using IntStream boxed() to return
        // a Stream consisting of the elements
        // of this stream, each boxed to an Integer.
        Stream<Integer> stream1 = stream.boxed();
  
        Stream<Object> stream2 = Stream.concat(stream1,
                                    Stream.of("Geeks", "for", "Geeks"));
  
        // Displaying the elements
        stream2.forEach(System.out::println);
    }
}

Producción :

3
4
5
6
7
Geeks
for
Geeks

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

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *