Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaIntStream of() in Java

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 : 
 

Java




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


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 : 
 

Java




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


Output : 
 

-7
-9
-11

Example 2 : 
 

Java




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


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());//Trying to use a stream that has been closed previously
        }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 $[]){
        //Reusing a stream
        Supplier<IntStream>supplier=()->IntStream.of(343,434,61,1,512,
                                                     5234,613434,561);
        //supplier.get() will return an instance of IntStream
        //sorting -> printing each value
        supplier.get()
            .sorted()
            .forEach(System.out::println);
        out.println();
       
        //filtering even numbers -> sorting -> printing each value
        supplier.get()
            .filter(x->x%2==0)
            .sorted()
            .forEach(System.out::println);
        out.println();
         
      //filtering odd numbers -> boxing(converting to Integer) -> converted to List<Integer> -> streaming -> sorting in reverse order
        //-> printing each value
        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);
         
        //Average
        OptionalDouble avg=supplier.get().average();
        out.println("Average : "+avg.orElse(0));
        // Sum
        int sum=supplier.get().sum();
        out.println("Sum : "+sum);
        // Min
        OptionalInt min=supplier.get().min();
        out.println("min : "+min.orElse(0));
        // Max
        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 $[]){
 
        //Range
         
        //Will iterate from 0 to 5
        IntStream.range(0,6)
            .forEach(System.out::println);
        out.println();
 
        //Will iterate from 0 to 6
        IntStream.rangeClosed(0,6)
            .forEach(System.out::println);
        out.println();
         
        //rangeSum=0+1+2+3+...+99=4950
        int rangeSum=IntStream.range(0,100).sum();
        out.println("sum[0,100) : "+rangeSum);
         
        //rangeClosedSum=0+1+2+3+...+100=5050
        int rangeClosedSum=IntStream.rangeClosed(0,100).sum();
        out.println("sum[0,100] : "+rangeClosedSum);
    }
}


RELATED ARTICLES

Most Popular

Recent Comments