How to Find the Minimum or Maximum Element from the Vector in Java? Last Updated : 04 Apr, 2023 Comments Improve Suggest changes Like Article Like Report 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: Using Collection.min() and Collection.max() methods.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 elementCollections.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 elements 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 the vector System.out.println("Minimum Number in Vector is : " + minNumber); } } OutputVector 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 -minNumbermaxNumberIf 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 classes of // java.util package import java.util.*; // Importing Integer Wrapper class for // primitive data type import java.lang.Integer; // Class 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>(); // Adding 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); } } OutputVector elements : [100, 30, 7, 24, 13] Maximum Element in Vector : 100 Minimum Element in Vector : 7 Comment More infoAdvertise with us Next Article How to Find the Minimum or Maximum Element from the Vector in Java? R rohanchopra96 Follow Improve Article Tags : Java Java Programs Java-Vector Practice Tags : Java Similar Reads How to Find the Minimum or Maximum Element from LinkedHashSet in Java? Given a LinkedHashSet the task is to find the minimum and maximum element from this LinkedHashSet in Java. There are two ways to find the minimum or maximum element from LinkedHashSet in Java as follows Using the min and max method of the Collections classBy Iterating the LinkedHashSetExamples: Inpu 2 min read Java Program to Get the Maximum Element From a Vector Prerequisite: Vectors in Java Why We Use Vector? Till now, we have learned two ways for declaring either with a fixed size of array or size enter as per the demand of the user according to which array is allocated in memory. int Array_name[Fixed_size] ; int array_name[variable_size] ; Both ways we l 4 min read How to Get First and Last Element From the Vector in Java? The first element in the Vector can be obtained using the firstElement() method of vector class that is represent util package. The last element in the Vector can be obtained using the lastElement() method of vector class that is also represent util package. Neither of these methods requires any par 2 min read Finding Minimum Element of Java Vector Vector implements a dynamic array that means it can grow or shrink as required. Like an array, it contains components that can be accessed using an integer index. We know two ways for declaring array i.e. either with a fixed size of array or size enter as per the demand of the user according to whic 3 min read Finding Maximum Element of Java Vector Vector implements a dynamic array that means it can grow or shrink as required. Like an array, it contains components that can be accessed using an integer index. We know two ways for declaring array i.e. either with a fixed size of array or size enter as per the demand of the user according to whic 3 min read Like