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.
1 |
public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> collection) |
The Collections max
method returns the maximum element of the collection according to the natural order of the elements.
1 |
public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> collection) |
Similarly, the min
method returns the minimum element of the specified collection object according to the natural order of the elements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Collections; import java.util.Vector; public class FindMinMaxVectorElement { public static void main(String[] args) { Vector<Integer> vNumbers = new Vector<Integer>(); vNumbers.add(32); vNumbers.add(29); vNumbers.add(35); vNumbers.add(33); vNumbers.add(31); //use the max method to find the maximum element System.out.println("Max element: " + Collections.max(vNumbers)); //use the min method to find the minimum element System.out.println("Min element: " + Collections.min(vNumbers)); } } |
Output
1 2 |
Max element: 35 Min element: 29 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Vector<Integer> vNumbers = new Vector<Integer>(); vNumbers.add(32); vNumbers.add(29); vNumbers.add(35); vNumbers.add(33); vNumbers.add(31); /* * This will return 2 i.e. index of * the biggest or largest element 35 */ System.out.println( vNumbers.indexOf( Collections.max(vNumbers) ) ); /* * This will return 1 i.e. index of * the smallest or lowest element 29 */ System.out.println( vNumbers.indexOf( Collections.min(vNumbers) ) ); |
Output
1 2 |
2 1 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import java.util.Collections; import java.util.Vector; /* * Step 1: Implement Comparable interface * in your custom class */ class Person implements Comparable<Person>{ private int age; public Person(int age){ this.age = age; } public String toString(){ return "Person[" + this.age + "]"; } public int getAge(){ return this.age; } /* * Step 2: Define the compareTo method * * This method compares the Person objects based * on the age field. */ public int compareTo(Person anotherPerson) { return this.getAge() - anotherPerson.getAge(); } } public class FindMinMaxVectorElement { public static void main(String[] args) { Vector<Person> vPersons = new Vector<Person>(); vPersons.add(new Person(34)); vPersons.add(new Person(45)); vPersons.add(new Person(29)); vPersons.add(new Person(43)); vPersons.add(new Person(21)); System.out.println("Min Person object: " + Collections.min(vPersons)); System.out.println("Max Person object: " + Collections.max(vPersons)); } } |
Output
1 2 |
Min Person object: Person[21] Max Person object: Person[45] |
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