Stream flatMapToLong() in Java with examples

Stream flatMapToLong(Function mapper) applies given mapper function to each element of a stream and returns a LongStream. Stream flatMapToLong(Function mapper) is an intermediate operation. Intermediate operations are invoked on a Stream instance and after they finish their processing, they give a Stream instance as output.

Note : Each mapped stream is closed after its contents have been placed into this stream. If a mapped stream is null, an empty stream is used, instead.
Syntax :

LongStream flatMapToLong(Function<? super T, ? extends LongStream> 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 : flatMapToLong() with operation of parsing string to long.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for Stream flatMapToLong
// (Function mapper) to get an LongStream
// consisting of the results of replacing
// each element of this stream with the
// contents of a mapped stream.
import java.util.*;
import java.util.stream.LongStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Creating a list of Strings
        List<String> list = Arrays.asList("1", "2", "3",
                                          "4", "5");
  
        // Using Stream flatMapToLong(Function mapper)
        list.stream().flatMapToLong(num -> 
                     LongStream.of(Long.parseLong(num))).
                            forEach(System.out::println);
    }
}

chevron_right


Output :

1
2
3
4
5

Example 2 : flatMapToLong() with operation of mapping string with string length.



filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for Stream flatMapToLong
// (Function mapper) to get an LongStream
// consisting of the results of replacing
// each element of this stream with the
// contents of a mapped stream.
import java.util.*;
import java.util.stream.LongStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Creating a List of Strings
        List<String> list = Arrays.asList("Geeks", "GFG",
                                 "GeeksforGeeks", "gfg");
  
        // Using Stream flatMapToLong(Function mapper)
        // to get length of all strings present in list
        list.stream().flatMapToLong(str -> 
                        LongStream.of(str.length())).
                        forEach(System.out::println);
    }
}

chevron_right


Output :

5
3
13
3

Example 3 : flatMapToLong() function with operation of returning stream.

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.*;
import java.util.stream.LongStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating a list of Strings
        List<String> list = Arrays.asList("5.36","27.9",
                                         "3","4.2","5");
  
        // Using Stream mapToLong(ToLongFunction mapper)
        // and displaying the corresponding LongStream
        list.stream().flatMapToLong(n
                     -> LongStream.of(Long.parseLong(n)) )
                     .forEach(System.out::println);
    }
}

chevron_right


Output :

Exception in thread "main" java.lang.NumberFormatException: For input string: "5.36"

Note : A Java NumberFormatException usually occurs when you try to do something like converting a String to a numeric value, like an int, float, double, long etc.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.