Java Program to Get the Maximum Element From a Vector
Last Updated :
07 Jul, 2021
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 land up wasting memory so in order to properly utilize memory optimization vectors are introduced.
Advantages Of Using Vectors
- Dynamic Size
- Rich Library Functions
- Easy To Know Size
- No Need To Pass Size
- Can be returned from a function
- By default initializes with default values
Rich Library Functions Includes
- Find An Element
- Erase An Element
- Insert An Element
Here, we use rich library functions to get a maximum element.
Note: Arrays are always passed as a pointer in arrays so another parameter you must pass the size of the array but that is not required in the case of vectors.
Syntax :
In the case of Arrays
type function_Name(type arrayName[], type sizeOfArray) ;
In the case of Vectors
type function_Name(vector<type> vectorName) ;
Considering An Example given a vector, the task is to find the maximum element.
Examples:
Input: v1={1,2,3,4,5}
Output: 5
Input: v2={7,50,0,67,98}
Output: 98
Method 1: Using a Predefined Function
- First, we will initialize a vector lets say v, then we will store values in that vector.
- After that, we will call the predefined method called max() defined in class java.util.Collections.
- Print the max element.
Below is the implementation of the above approach.
Java
// Java Program to find maximum element
// in a vector using predefined method
import java.io.*;
// Importing Vector Class
import java.util.Collections;
// Importing Vector Class
import java.util.Vector;
class GFG {
// Main Method
public static void main(String[] args)
{
// initializing a Vector
Vector<Integer> v = new Vector<Integer>();
// adding values to the Vector
v.add(7);
v.add(50);
v.add(0);
v.add(67);
v.add(98);
// finding the largest element
int n = Collections.max(v);
// printing the largest element
System.out.println(
"The maximum value present in Vector is : "
+ n);
}
}
Output :
The maximum value present in Vector is : 98
Worst Case Time Complexity: O(n) where n is the number of elements present in the vector.
Method 2: Comparing each element present in Vector
- First, we will initialize a vector lets say v, then we will store values in that vector.
- Next, we will take a variable, let us say maxNumber and assign the minimum value possible.
- Traverse till the end of vector and compare each element of a vector with maxNumber.
- If the element present in the vector is greater than maxNumber, then update maxNumber to that value.
- Print maxNumber.
Below is the implementation of the above approach.
Java
// Java program to find largest element
// present in Vector via comparison
import java.io.*;
// Importing Vector Class
import java.util.Vector;
// Importing Iterator Class
import java.util.Iterator;
class GFG {
// Main Method
public static void main(String[] args)
{
// initializing vector of Integer type
Vector<Integer> v = new Vector<Integer>();
// Adding elements in vector
v.add(1);
v.add(2);
v.add(3);
v.add(4);
v.add(5);
// Assigning min value possible
int maxValue = Integer.MIN_VALUE;
// Creating an iterator to traverse through vector
// in the beginning itr will point to index just
// before first element
Iterator itr = v.iterator();
// See if there is any next element
while (itr.hasNext())
{
// Moving iterator to next element
int element = (Integer)itr.next();
// Comparing if element is larger than maxValue
if (element > maxValue)
{
// Update maxValue
maxValue = element;
}
}
// Print maxVaue
System.out.println(
"The largest element present in Vector is : "
+ maxValue);
}
}
Output :
The largest element present in Vector is : 5
Time Complexity: O(n) where n is the number of elements present in the vector.
Similar Reads
Java Program to Find the Maximum Element in a Matrix Given a multi-dimensional 2D matrix of n rows and m column order N Ã M. The task is to find the maximum element in the given matrix. Illustration: Input : mat[][] = { {1,3,4,19}, {11,10,12,1}, {7,9,0,4,99} } Output : 99 Methods: Iterative method (naive approach)Using the principle of recursion (Bit
3 min read
Java Program to Search an Element in Vector A vector in Java is a dynamic array that can be resized as needed. It is synchronized, which means it is safe in multi-threaded programs. To find an element in a Vector we have to loop through its elements to find a match. In this article, we will learn how to search for a component in a Vector usin
3 min read
How to Find the Minimum or Maximum Element from the Vector in Java? 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
3 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 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