LongStream boxed() in Java
Last Updated :
06 Dec, 2018
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 :
- Stream : A sequence of elements supporting sequential and parallel aggregate operations.
- 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 :
import java.util.*;
import java.util.stream.Stream;
import java.util.stream.LongStream;
class GFG {
public static void main(String[] args)
{
LongStream stream = LongStream.range(3L, 8L);
Stream<Long> stream1 = stream.boxed();
stream1.forEach(System.out::println);
}
}
|
Output :
3
4
5
6
7
Example 2 :
import java.util.*;
import java.util.stream.Stream;
import java.util.stream.LongStream;
class GFG {
public static void main(String[] args)
{
LongStream stream = LongStream.range(3L, 8L);
Stream<Long> stream1 = stream.boxed();
Stream<Object> stream2 = Stream.concat(stream1,
Stream.of( "Geeks" , "for" , "Geeks" ));
stream2.forEach(System.out::println);
}
}
|
Output :
3
4
5
6
7
Geeks
for
Geeks
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...