Thursday, December 18, 2025
HomeLanguagesJavaIntStream boxed() in Java

IntStream boxed() in Java

IntStream boxed() returns a Stream consisting of the elements of this stream, each boxed to an Integer.

Note : IntStream boxed() is a intermediate operation. These operations are always lazy. Intermediate operations are invoked on a Stream instance and after they finish their processing, they give a Stream instance as output.
Syntax :

Stream<Integer> boxed()

Parameters :

  1. Stream : A sequence of elements supporting sequential and parallel aggregate operations.
  2. Integer : The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.

Return Value : The function returns a Stream boxed to an Integer.

Example 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);
    }
}


Output :

3
4
5
6
7

Example 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);
    }
}


Output :

3
4
5
6
7
Geeks
for
Geeks
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32455 POSTS0 COMMENTS
Milvus
108 POSTS0 COMMENTS
Nango Kala
6823 POSTS0 COMMENTS
Nicole Veronica
11958 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12037 POSTS0 COMMENTS
Shaida Kate Naidoo
6958 POSTS0 COMMENTS
Ted Musemwa
7203 POSTS0 COMMENTS
Thapelo Manthata
6911 POSTS0 COMMENTS
Umr Jansen
6890 POSTS0 COMMENTS