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 :
- IntStream : A sequence of primitive int-valued elements.
- t : Represents the single element in the IntStream.
Return Value : IntStream of(int t) returns a sequential IntStream containing the single specified element.
Example :
Java
import java.util.*;
import java.util.stream.IntStream;
class GFG {
public static void main(String[] args)
{
IntStream stream = IntStream.of(- 7 );
stream.forEach(System.out::println);
}
}
|
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 :
- IntStream : A sequence of primitive int-valued elements.
- 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 :
Java
import java.util.*;
import java.util.stream.IntStream;
class GFG {
public static void main(String[] args)
{
IntStream stream = IntStream.of(- 7 , - 9 , - 11 );
stream.forEach(System.out::println);
}
}
|
Output :
-7
-9
-11
Example 2 :
Java
import java.util.*;
import java.util.stream.IntStream;
class GFG {
public static void main(String[] args)
{
IntStream stream = IntStream.of(- 7 , - 9 , - 11 );
long total = stream.count();
System.out.println(total);
}
}
|
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
Java
import java.util.stream.IntStream;
import java.io.PrintStream;
class GFG{
static final PrintStream out=System.out;
public static void main(String $[]){
IntStream intStream=IntStream.of( 10 , 20 , 30 );
out.println(intStream.min());
try {
out.println(intStream.min());
} catch (IllegalStateException e){
out.println(e);
}
}
}
|
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.
Java
import java.util.stream.IntStream;
import java.io.PrintStream;
import java.util.function.Supplier;
import java.util.List;
import java.util.stream.Collectors;
public class IntStreamClass{
static final PrintStream out=System.out;
public static void main(String $[]){
Supplier<IntStream>supplier=()->IntStream.of( 343 , 434 , 61 , 1 , 512 ,
5234 , 613434 , 561 );
supplier.get()
.sorted()
.forEach(System.out::println);
out.println();
supplier.get()
.filter(x->x% 2 == 0 )
.sorted()
.forEach(System.out::println);
out.println();
supplier.get()
.filter(x->(x& 1 )== 0 )
.boxed()
.collect(Collectors.toList())
.stream()
.sorted((a,b)->b-a)
.forEach(System.out::println);
}
}
|
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
Java
import java.util.stream.IntStream;
import java.io.PrintStream;
import java.util.function.Supplier;
import java.util.OptionalInt;
import java.util.OptionalDouble;
public class IntStreamClass{
static final PrintStream out=System.out;
public static void main(String $[]){
Supplier<IntStream>supplier=()->IntStream.of( 343 , 434 , 61 , 1 , 512 ,
5234 , 613434 , 561 );
OptionalDouble avg=supplier.get().average();
out.println( "Average : " +avg.orElse( 0 ));
int sum=supplier.get().sum();
out.println( "Sum : " +sum);
OptionalInt min=supplier.get().min();
out.println( "min : " +min.orElse( 0 ));
OptionalInt max=supplier.get().max();
out.println( "max : " +max.orElse( 0 ));
}
}
|
Output :
Average : 77572.5
Sum : 620580
min : 1
max : 613434
Example 5
Range based operation
Java
import java.util.stream.IntStream;
import java.io.PrintStream;
import java.util.function.Supplier;
import java.util.OptionalInt;
import java.util.OptionalDouble;
public class IntStreamClass{
static final PrintStream out=System.out;
public static void main(String $[]){
IntStream.range( 0 , 6 )
.forEach(System.out::println);
out.println();
IntStream.rangeClosed( 0 , 6 )
.forEach(System.out::println);
out.println();
int rangeSum=IntStream.range( 0 , 100 ).sum();
out.println( "sum[0,100) : " +rangeSum);
int rangeClosedSum=IntStream.rangeClosed( 0 , 100 ).sum();
out.println( "sum[0,100] : " +rangeClosedSum);
}
}
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...