📜  Java中的 LongStream asDoubleStream()

📅  最后修改于: 2022-05-13 01:55:11.121000             🧑  作者: Mango

Java中的 LongStream asDoubleStream()

LongStream asDoubleStream()返回一个由该流的元素组成的DoubleStream ,转换为双精度。这是一个中间操作。在 Stream 实例上调用中间操作,在它们完成处理后,它们给出一个 Stream 实例作为输出。

句法 :

DoubleStream asDoubleStream()

Where, DoubleStream is a sequence of 
primitive double-valued element.

返回值: LongStream asDoubleStream() 返回一个由该流的元素组成的 DoubleStream,转换为双精度。

示例 1:

// Java code for DoubleStream asDoubleStream()
// to return a DoubleStream consisting of
// the elements of this stream
import java.util.*;
import java.util.stream.LongStream;
import java.util.stream.DoubleStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating a LongStream
        LongStream stream = LongStream.of(3L, 5L, 9L, 12L, 14L);
  
        // Using DoubleStream asDoubleStream()
        DoubleStream stream1 = stream.asDoubleStream();
  
        // Displaying DoubleStream consisting of
        // the elements of this stream
        stream1.forEach(System.out::println);
    }
}

输出 :

3.0
5.0
9.0
12.0
14.0

示例 2:

// Java code for DoubleStream asDoubleStream()
// to return a DoubleStream consisting of
// the elements of this stream
import java.util.*;
import java.util.stream.LongStream;
import java.util.stream.DoubleStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an LongStream and using asDoubleStream()
        DoubleStream stream = LongStream.range(3L, 8L)
                                  .asDoubleStream();
  
        // Displaying DoubleStream consisting of
        // the elements of this stream
        stream.forEach(System.out::println);
    }
}

输出 :

3.0
4.0
5.0
6.0
7.0

示例 3:

// Java code for DoubleStream asDoubleStream()
// to return a DoubleStream consisting of
// the elements of this stream
import java.util.*;
import java.util.stream.LongStream;
import java.util.stream.DoubleStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating a LongStream and using asDoubleStream()
        // to display the elements of LongStream and
        // DoubleStream together
        DoubleStream stream = LongStream.range(3L, 8L)
                                  .peek(System.out::println)
                                  .asDoubleStream();
  
        // Displaying DoubleStream consisting of
        // the elements of this stream
        stream.forEach(System.out::println);
    }
}

输出 :

3
3.0
4
4.0
5
5.0
6
6.0
7
7.0