Find Minimum or Maximum Element from Vector in Java Example

This example shows how to find the minimum or maximum element from the Vector in Java. This example also shows how to find a min or max element using the Collections class.

How to find the minimum or maximum element from Vector in Java?

The Java Collections class provides many utility methods that operate on the collection classes including finding minimum and maximum values in the collection object.

The Collections max method returns the maximum element of the collection according to the natural order of the elements.

Similarly, the min method returns the minimum element of the specified collection object according to the natural order of the elements.

Output

How to find the index of min or max Vector elements?

We can use the indexOf method along with the min or max method to find the index of the minimum or maximum element of the vector object as given below.

Output

How to find min/max elements from the Vector of custom class objects?

If the vector elements are objects of a custom class, then the custom class must implement the Comparable interface for the min or max method to work. Another option is to provide a custom Comparator object in the overloaded min and max methods that accept a comparator object.

I am going to show how to find the minimum or maximum element from the vector of a custom class objects using the Comparable interface.

Output

This example is a part of the Java Vector Tutorial with Examples.

Please let me know your views in the comments section below.

References:
Java 8 Vector Documentation

About the author

RahimV

RahimV

My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.

Add Comment

Your email address will not be published. Required fields are marked *