LongStream mapToObj() method in Java


The LongStream mapToObj() method is used in Java to return an object-valued Stream consisting of the results of applying the given function to the elements of this stream.

The syntax is as follows:

<U> Stream<U>mapToObj(LongFunction<? extends U> mapper)

Here, Stream is a sequence of elements. The LongFunction represents a function that accepts a longvalued argument and produces a result. Parameter mapper is a stateless function to apply to each element. The <U> represents the element type of the new stream.</p>

To use the LongStream class in Java, import the following package:

import java.util.stream.LongStream;

The following is an example to implement LongStream mapToObj():

Example

import java.util.stream.Stream;
import java.util.stream.LongStream;
public class Demo {
   public static void main(String[] args) {
      LongStream longStream = LongStream.range(10L, 15L);
      System.out.println("Binary Representation...");
      Stream<String> s = longStream.mapToObj(a→ Long.toBinaryString(a));
      s.forEach(System.out::println);
   }
}

output

Binary Representation...
1010
1011
1100
1101
1110

Updated on: 30-Jul-2019

101 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements