LongStream boxed() method in Java

Java 8Object Oriented ProgrammingProgramming

The boxed() method of the LongStream class returns a Stream consisting of the elements of this stream, each boxed to a Long.

The syntax is as follows.

Stream<Long> boxed()

Here, Stream represents a sequence of elements. To use the LongStream class in Java, import the following package.

import java.util.stream.LongStream;

Create an IntStream and add some elements in a range using the range() method.

LongStream intStream = LongStream.range(20800L, 20805L);

Now, use the boxed() method

Stream<Long> s = intStream.boxed();

The following is an example to implement LongStream boxed() method in Java.

Example

 Live Demo

import java.util.stream.Stream;
import java.util.stream.LongStream;
public class Demo {
   public static void main(String[] args) {
      LongStream intStream = LongStream.range(20800L, 20805L);
      Stream<Long> s = intStream.boxed();
      s.forEach(System.out::println);
   }
}

Output

20800
20801
20802
20803
20804
raja
Published on 27-Mar-2019 10:26:59
Advertisements