Stream mapToDouble(ToDoubleFunction映射器)返回DoubleStream,該DoubleStream包含將給定函數應用於此 Stream 的元素的結果。
Stream mapToDouble(ToDoubleFunction映射器)是一個中間操作。這些操作總是很懶。在Stream實例上調用中間操作,並在完成處理後將中間實例作為輸出提供。
用法:
DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper) Where, A sequence of primitive double-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:mapToDouble()具有選擇滿足給定函數的元素的操作。
// Java code for Stream mapToDouble
// (ToDoubleFunction mapper) to get a
// DoubleStream 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("10", "6.548", "9.12",
"11", "15");
// Using Stream mapToDouble(ToDoubleFunction mapper)
// and displaying the corresponding DoubleStream
list.stream().mapToDouble(num -> Double.parseDouble(num))
.filter(num -> (num * num) * 2 == 450)
.forEach(System.out::println);
}
}
輸出:
15.0
範例2:mapToDouble()具有返回具有字符串長度平方的 Stream 的操作。
// Java code for Stream mapToDouble
// (ToDoubleFunction mapper) to get a
// DoubleStream 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("CSE", "JAVA", "gfg",
"C++", "C");
// Using Stream mapToDouble(ToDoubleFunction mapper)
// and displaying the corresponding DoubleStream
// which contains square of length of each element in
// given Stream
list.stream().mapToDouble(str -> str.length() * str.length())
.forEach(System.out::println);
}
}
輸出:
9.0 16.0 9.0 9.0 1.0
相關用法
- Java LongStream mapToDouble()用法及代碼示例
- Java IntStream mapToDouble()用法及代碼示例
- Java Stream map()用法及代碼示例
- Java Stream flatMapToLong()用法及代碼示例
- Java Stream flatMapToInt()用法及代碼示例
- Java Stream min()用法及代碼示例
- Java Stream peek()用法及代碼示例
- Java Stream mapToInt()用法及代碼示例
- Java Stream mapToLong()用法及代碼示例
- Java Stream builder()用法及代碼示例
- Java Stream.max()用法及代碼示例
- Java Stream.reduce()用法及代碼示例
- Java Stream empty()用法及代碼示例
注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 Stream mapToDouble() in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。