LongStream boxed() in Java

LongStream boxed() returns a Stream consisting of the elements of this stream, each boxed to a Long.
Note : LongStream 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<Long> boxed()

Parameters :

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

Return Value : The function returns a Stream boxed to a Long.

Example 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :



3
4
5
6
7

Example 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

3
4
5
6
7
Geeks
for
Geeks

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts: