Skip to content
Related Articles

Related Articles

How to Find the Minimum or Maximum Element from the Vector in Java?
  • Last Updated : 07 Jan, 2021

The Collection is a framework offered by Java that provides an architecture to store a group of objects. One such collection is Vector(). There are many ways through which we can find the minimum and maximum elements in a Vector. These methods have been discussed below:

Methods:

  1. Using Collection.min() and Collection.max() methods.
  2. Using Iterating over vector elements setting min-max.

Method 1: Using Collections.min() and Collections.max() method

Collections package offers a static method of finding the minimum and maximum in a collection. These methods are Collections.max() and Collections.min(). 

  • Collections.max() finds the maximum element
  • Collections.min() finds the minimum element in a collection.

Example



Java




// Java Program to Find minimum and maximum elements
// from a Vector
  
// Importing all classes of
// java.util package
import java.util.*;
  
// Class to find min and max
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Creating(defining) a  Vector
        Vector<Integer> V = new Vector<Integer>();
  
        // Add elemetns in Vector
        // Custom inouts
        V.add(1);
        V.add(2);
        V.add(3);
        V.add(4);
        V.add(5);
  
        // Printing all elements of vector
        System.out.println("Vector elements : " + V);
        // Using inbuilt function to
  
        // find minimum number
        // using Collection.min() method
        int minNumber = Collections.min(V);
  
        // find maximum number
        // using Collection.max() method
        int maxNumber = Collections.max(V);
  
        // Print max element of the vector
        System.out.println("Maximum Number in Vector is : "
                           + maxNumber);
  
        // Print min element of th vector
        System.out.println("Minimum Number in Vector is : "
                           + minNumber);
    }
}
Output
Vector elements : [1, 2, 3, 4, 5]
Maximum Number in Vector is : 5
Minimum Number in Vector is : 1

Method 2: Using Iterating over vector elements setting min-max.

Approach:

  • Take a variable say minNumber, and initialized to the maximum value later on to compare and update.
  • Take a variable say maxNumber, and initialized to minimum value later on to compare an update.
  • Iterate through Vector and compare each element with the above two-variable –
    • minNumber
    • maxNumber
  • If the number is less than minNumber, update the value of minNumber.
  • If the number is greater than maxNumber, update the value of maxNumber.

Example

Java




// Java Program to Find minimum and maximum elements
// from a Vector
  
// Importing all classses of
// java.util package
import java.util.*;
// Importing Integer Wrapper class for
// primitive data type
import java.lang.Integer;
  
// Clas
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Assign maximum value to minValue Variable
        int minValue = Integer.MAX_VALUE;
  
        // Assign minimum value to maxValue Variable
        int maxValue = Integer.MIN_VALUE;
  
        // Creating a Vector
        Vector<Integer> V = new Vector<Integer>();
  
        // Addiing elements into Vector
        // Custom inputs
        V.add(100);
        V.add(30);
        V.add(7);
        V.add(24);
        V.add(13);
  
      System.out.println("Vector elements : "+V);
        // For-each loop to traverse through vector
  
        // If element is present in vector
        for (Integer i : V) {
  
            // if greater than maxValue, then update
            if (i > maxValue) {
                maxValue = i;
            }
  
            // if less than minValue, then update
            if (i < minValue) {
                minValue = i;
            }
        }
  
        // Print maximum element in the Vector
        System.out.println("Maximum Element in Vector : "
                           + maxValue);
  
        // Print minimum element in the vector
        System.out.println("Minimum Element in Vector : "
                           + minValue);
    }
}
Output
Vector elements : [100, 30, 7, 24, 13]
Maximum Element in Vector : 100
Minimum Element in Vector : 7

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :