Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Finding Maximum Element of Java Vector
Next article icon

Finding Maximum Element of Java Vector

Last Updated : 05 Jul, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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 which array is allocated in memory. 

int Array_name[Fixed_size] ;  int array_name[variable_size] ;

In both ways, we land up wasting memory so, in order to properly utilize memory optimization, Vectors were introduced. We have to find out the maximum element in the given Java vector.

Examples:

Input: [1,2,3,4,5] Output: 5 Input: [88,23,76,90,56] Output: 90

Approach 1: Using a Predefined Function

  • To find the minimum element of a given vector we use the java.util.Collections.max() method.
  • This directly finds the maximum value in the vector.
Java
// Java program to find // maximum element in java vector  import java.io.*; import java.util.Collections; import java.util.Vector; public class GFG {      public static void main(String args[])          Vector<Integer> vec = new Vector<Integer>();          vec.add(1);         vec.add(2);         vec.add(3);         vec.add(4);         vec.add(5);          System.out.println("Vector elements: " + vec);            System.out.println("The maximum element of the Vector is: "          + Collections.max(vec));     }  } 

Output
Vector elements: [1, 2, 3, 4, 5] The maximum element of the Vector is: 5

Worst Case Time Complexity: O(n) where n is the number of elements present in the vector.

Approach 2: Comparing each element present in Vector 

  • First, we will initialize a vector let's say v, then we will store values in that vector.
  • Next, we will take a variable, let us say maxNumber and assign the maximum 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.
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(10);          v.add(20);          v.add(30);          v.add(40);          v.add(50);                   // 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 : 50

Time Complexity: O(n) where n is the number of elements present in the vector.


 


Next Article
Finding Maximum Element of Java Vector

P

pravallika26
Improve
Article Tags :
  • Java
  • Java Programs
  • Java-Vector
Practice Tags :
  • Java

Similar Reads

    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
    Replacing Element in Java Vector
    To replace an element in Java Vector, set() method of java.util.Vector class can be used. The set() method takes two parameters-the indexes of the element which has to be replaced and the new element. The index of a Vector is zero-based. So, to replace the first element, 0 should be the index passed
    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 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
    Iterate Over Vector Elements in Java
    Vector is like the dynamic array which can grow or shrink its size. Unlike array, we can store n-number of elements in it as there is no size limit. We can iterate over vector by the following ways: Simple for-loopEnhanced for-loopIteratorsEnumeration interface Method 1: Simple for-loop The idea is
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences