java map double_Java Stream mapToDouble()用法及代码示例

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 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 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值