Wednesday, July 3, 2024
HomeLanguagesJavaIntStream min() in Java with Examples

IntStream min() in Java with Examples

java.util.stream.IntStream in Java 8, deals with primitive ints. It helps to solve the old problems like finding maximum value in array, finding minimum value in array, sum of all elements in array, and average of all values in array in a new way. IntStream min() returns an OptionalInt describing the minimum element of this stream, or an empty optional if this stream is empty.

Syntax :

OptionalInt() min()

Where, OptionalInt is a container object which 
may or may not contain a int value.

Below given are some examples to understand the function in a better way.
Example 1 :




// Java code for IntStream min()
import java.util.*;
import java.util.stream.IntStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // creating a stream
        IntStream stream = IntStream.of(-9, -18, 54, 8, 7, 14, 3);
  
        // OptionalInt is a container object
        // which may or may not contain a
        // int value.
        OptionalInt obj = stream.min();
  
        // If a value is present, isPresent() will
        // return true and getAsInt() will
        // return the value
        if (obj.isPresent()) {
            System.out.println(obj.getAsInt());
        }
        else {
            System.out.println("-1");
        }
    }
}


Output :

-18

Example 2 :




// Java code for IntStream min()
// to get the minimum value in range
// excluding the last element
import java.util.*;
import java.util.stream.IntStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // To find minimum in given range
        IntStream stream = IntStream.range(50, 75);
  
        // storing the minimum value in variable
        // if it is present, else show -1.
        int minimum = stream.min().orElse(-1);
  
        // displaying the minimum value
        System.out.println(minimum);
    }
}


Output :

50

Example 3 :




// Java code for IntStream min()
// to get the minimum value in range
// excluding the last element
import java.util.*;
import java.util.stream.IntStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // To find minimum in given range
        IntStream stream = IntStream.range(50, 50);
  
        // storing the minimum value in variable
        // if it is present, else show -1.
        int minimum = stream.min().orElse(-1);
  
        // displaying the minimum value
        System.out.println(minimum);
    }
}


Output :

-1

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments