Wednesday, October 15, 2025
HomeLanguagesJavaLongStream of() in Java

LongStream of() in Java

LongStream of(long t)

LongStream of(long t) returns a sequential LongStream containing a single element.
Syntax :

static LongStream of(long t)

Parameters :

  1. LongStream : A sequence of primitive long-valued elements.
  2. t : Represents the single element in the LongStream.

Return Value : LongStream of(long t) returns a sequential LongStream containing the single specified element.

Example :




// Java code for LongStream of(long t)
// to get a sequential LongStream
// containing a single element.
import java.util.*;
import java.util.stream.LongStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an LongStream having single element only
        LongStream stream = LongStream.of(-7L);
  
        // Displaying the LongStream having single element
        stream.forEach(System.out::println);
    }
}


Output:

-7

LongStream of(long… values)

LongStream of(long… values) returns a sequential ordered stream whose elements are the specified values.
Syntax :

static LongStream of(long... values)

Parameters :

  1. LongStream : A sequence of primitive long-valued elements.
  2. values : Represents the elements of the new stream.

Return Value : LongStream of(long… values) returns a sequential ordered stream whose elements are the specified values.

Example 1 :




// Java code for LongStream of(long... values)
// to get a sequential ordered stream whose
// elements are the specified values.
import java.util.*;
import java.util.stream.LongStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an LongStream
        LongStream stream = LongStream.of(-7L, -9L, -11L);
  
        // Displaying the sequential ordered stream
        stream.forEach(System.out::println);
    }
}


Output:

-7
-9
-11

Example 2 :




// Java code for LongStream of(long... values)
// to get a sequential ordered stream whose
// elements are the specified values.
import java.util.*;
import java.util.stream.LongStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an LongStream
        LongStream stream = LongStream.of(-7L, -9L, -11L);
  
        // Storing the count of elements in LongStream
        long total = stream.count();
  
        // Displaying the count of elements
        System.out.println(total);
    }
}


Output:

3
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11952 POSTS0 COMMENTS
Shaida Kate Naidoo
6851 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS