首先,我们设置了流-
Stream.of(3.5, 4.7, 7.9, 8.2, 9.1, 10.5, 12.3, 15.8)
现在要将double映射到int Object,请使用mapToObj-
.mapToInt(Double::intValue) .mapToObj(a -> "val" + a)
以下是使用mapToInt和mapToObj将double映射到int对象的示例-
import java.util.stream.Stream; public class Demo { public static void main(String[] args) throws Exception { Stream.of(3.5, 4.7, 7.9, 8.2, 9.1, 10.5, 12.3, 15.8) .mapToInt(Double::intValue) .mapToObj(a -> "val" + a) .forEach(System.out::println); } }
输出结果
val3 val4 val7 val8 val9 val10 val12 val15