VOOZH about

URL: https://www.geeksforgeeks.org/java/intstream-of-in-java/

⇱ IntStream of() in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

IntStream of() in Java

Last Updated : 18 May, 2021

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 : 
 

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 : 
 

Output : 
 

-7
-9
-11


Example 2 : 
 

Output : 
 

3


 Example 3

Streams are not reusable if we have performed terminal operation on stream and try to reuse them again IllegalStateExeception will be generated 

Output :

OptionalInt[10]

java.lang.IllegalStateException: stream has already been operated upon or closed

To reuse a stream  we need Supplier class when get() method of Supplier is called every time it will generate a new instance and return it.

Output :

1
61
343
434
512
561
5234
613434

434
512
5234
613434

613434
5234
512
434

Example 4

Finding min,max,sum and average from an IntStream

Output :

Average : 77572.5
Sum : 620580
min : 1
max : 613434

Example 5

Range based operation

Comment