Wednesday, July 3, 2024
HomeLanguagesJavaLongStream asDoubleStream() in Java

LongStream asDoubleStream() in Java

LongStream asDoubleStream() returns a DoubleStream consisting of the elements of this stream, converted to double. This is an intermediate operation. Intermediate operations are invoked on a Stream instance and after they finish their processing, they give a Stream instance as output.

Syntax :

DoubleStream asDoubleStream()

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

Return Value : LongStream asDoubleStream() returns a DoubleStream consisting of the elements of this stream, converted to double.

Example 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);
    }
}


Output :

3.0
5.0
9.0
12.0
14.0

Example 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);
    }
}


Output :

3.0
4.0
5.0
6.0
7.0

Example 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);
    }
}


Output :

3
3.0
4
4.0
5
5.0
6
6.0
7
7.0

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments