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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。