Stream mapToLong() in Java with examples

Stream mapToLong(ToLongFunction mapper) returns a LongStream consisting of the results of applying the given function to the elements of this stream.

Stream mapToLong(ToLongFunction mapper) is an 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 :



LongStream mapToLong(ToLongFunction<? super T> mapper)

Where, LongStream is a sequence of primitive 
long-valued elements and T is the type 
of stream elements. mapper is a stateless function 
which is applied to each element and the function
returns the new stream.

Example 1 : mapToLong() function with operation of returning stream satisfying the given function.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for Stream mapToLong
// (ToLongFunction mapper) to get a
// LongStream by applying the given function
// to the elements of this stream.
import java.util.*;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        System.out.println("The stream after applying "
                        + "the function is : ");
  
        // Creating a list of Strings
        List<String> list = Arrays.asList("25", "225", "1000",
                                                  "20", "15");
  
        // Using Stream mapToLong(ToLongFunction mapper)
        // and displaying the corresponding LongStream
        list.stream().mapToLong(num -> Long.parseLong(num))
            .filter(num -> Math.sqrt(num) / 5 == 3 )
            .forEach(System.out::println);
    }
}

chevron_right


Output :

The stream after applying the function is : 
225

Example 2 : mapToLong() function with returning number of set-bits in string length.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for Stream mapToLong
// (ToLongFunction mapper) to get a
// LongStream by applying the given function
// to the elements of this stream.
import java.util.*;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating a list of Strings
        List<String> list = Arrays.asList("Data Structures", "JAVA", "OOPS",
                                             "GeeksforGeeks", "Algorithms");
  
        // Using Stream mapToLong(ToLongFunction mapper)
        // and displaying the corresponding LongStream
        // which contains the number of one-bits in 
        // binary representation of String length
        list.stream().mapToLong(str -> Long.bitCount(str.length()))
            .forEach(System.out::println);
    }
}

chevron_right


Output :

4
1
1
3
2


My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.