How to Find Maximum Value of a Vector in Java


Here we will write an example to introduce how to find a maximum element of Vector with max method of Collections class in Java.

Source Code

package com.beginner.examples;

import java.util.Collections;
import java.util.Vector;

public class FindMaxElementOfVector {

	public static void main(String[] args) {

		Vector nums = new Vector();
		// Add elements
		nums.add(8);
		nums.add(12);
		nums.add(10);
		nums.add(7);

		// Use the Max () method in the Collections utility class to find the
		// maximum
		Integer maxNum = Collections.max(nums);

		System.out.println("Max element:" + maxNum);

	}

}

Output:

Max element:12

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments