Stream flatMapToLong(Function mapper)将给定的mapper函数应用于流的每个元素,并返回LongStream。 Stream flatMapToLong(Function mapper)是一个中间操作。在Stream实例上调用中间操作,并在完成处理后将中间实例作为输出提供。
注意:将每个映射流的内容放入该流后,将其关闭。如果映射的流为null,则使用空流。
用法:
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.
示例1:flatMapToLong()具有将字符串解析为long的操作。
// 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);
}
}
输出:
1 2 3 4 5
示例2:flatMapToLong()具有按字符串长度映射字符串的操作。
// 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);
}
}
输出:
5 3 13 3
示例3:flatMapToLong()函数,具有返回流的操作。
// 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);
}
}
输出:
Exception in thread "main" java.lang.NumberFormatException: For input string: "5.36"
注意:当您尝试执行将String转换为数值(例如int,float,double,long等)时,通常会发生Java NumberFormatException。
相关用法
- Java Stream map()用法及代码示例
- Java Stream mapToLong()用法及代码示例
- Java Stream findAny()用法及代码示例
- Java Stream allMatch()用法及代码示例
- Java Stream anyMatch()用法及代码示例
- Java Stream filter()用法及代码示例
- Java Stream.reduce()用法及代码示例
- Java Stream min()用法及代码示例
- Java Stream.max()用法及代码示例
- Java Stream toArray()用法及代码示例
- Java Stream empty()用法及代码示例
- Java Stream builder()用法及代码示例
- Java Stream flatMapToInt()用法及代码示例
- Java Stream findFirst()用法及代码示例
注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 Stream flatMapToLong() in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。