IntStream of() in Java

IntStream of(int t)

IntStream of(int t) returns a sequential IntStream containing a single element.
Syntax :

static IntStream of(int t)

Parameters :

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

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



Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

-7

IntStream of(int… values)

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

static IntStream of(int... values)

Parameters :

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

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

Example 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

-7
-9
-11

Example 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

3


My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.