Finding Minimum And Maximum Element of Vector using Comparable Interface in Java
Last Updated :
09 Aug, 2022
The Vector class in java implements a dynamic array i.e. it can grow and shrink according to the elements that we insert or remove to/from it. It implements the List interface so it supports all the methods provided by the List interface.
In this article, we are going to discuss how we can find the minimum and maximum elements of a Vector using the Comparable interface in Java.
We can use the Collections.min() and Collections.max() methods to find the minimum and maximum elements inside our Vector. But when we are using our custom class and we want to use Collections.min() and Collections.max() method then we first need to override the compareTo() method of the Comparable interface so that java is able to compare the instances of our custom class. Also since we want to compare the object, not the primitive data types, we have to use the custom-defined compareTo() method of the Comparable interface.
Syntax to implement Comparable<T> interface:
class Account implements Comparable<Account>
Using the above syntax we can implement the Comparable interface for our custom class, i.e. Account class.
Syntax to override CompareTo() method:
@Override
public int compareTo(Account o) {
// this refers to the current object(child object)
// and o is the parent object.
return this.balance - o.balance;
}
The above compareTo() compares the Account objects on the basis of their balance attribute. Our compareTo() method must return:
- Zero: when both Objects are equal.
- Negative value: when the current object is less than the passed object i.e. 'o'.
- Positive value: when the current object is greater than the passed object.
Below is the Code Implementation to find a minimum and maximum element of the vector using the comparable interface.
Java
// Java Program to find Minimum And Maximum Element of
// Vector using Comparable Interface in Java?
import java.util.Collections;
import java.util.Vector;
// The class Account implements the Comparable<T>
// interface
class Account implements Comparable<Account> {
private String name;
private int balance;
// Creating parameterized constructor for
// Account class
public Account(String name, int balance)
{
this.name = name;
this.balance = balance;
}
// Creating getters for Account class
public String getName() { return name; }
public int getBalance() { return balance; }
// We are overriding the CompareTo method of the
// Comparable interface to be able to compare the
// objects of the Account class CompareTo method should
// returns 0 when two Objects are equal, negative value
// when current object is less than 'o' otherwise
// returns positive value
@Override public int compareTo(Account o)
{
return this.balance - o.balance;
}
}
public class GFG {
public static void main(String[] args)
{
// Creating a Vector of Account type
Vector<Account> accountVector = new Vector<>();
// Inserting some Objects inside the accountVector
accountVector.add(new Account("Rahul", 10000));
accountVector.add(new Account("Anjali", 2000));
accountVector.add(new Account("Rohan", 5000));
accountVector.add(new Account("Pooja", 50000));
accountVector.add(new Account("Nandini", 50));
// Finding the Accounts with minimum and maximum
// balance using the Collections.min() and
// Collections.max() method
Account minAccount = Collections.min(accountVector);
Account maxAccount = Collections.max(accountVector);
// Displaying the account details of the
// Accounts having minimum and maximum balance
System.out.println(
minAccount.getName()
+ " has the minimum balance of Rs "
+ minAccount.getBalance());
System.out.println(
maxAccount.getName()
+ " has the maximum balance of Rs "
+ maxAccount.getBalance());
}
}
OutputNandini has the minimum balance of Rs 50
Pooja has the maximum balance of Rs 50000
Similar Reads
How to Sort Vector Elements using Comparable Interface in Java? Vector is a child interface of collection. If we want to represent a group of the individual objects as a single entity where duplicates are allowed and insertion order must be preserved then we should go for vector. It is a resizable or growable array. It implements a Serializable, Cloneable, and R
5 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
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
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
Finding the Minimum or Maximum Value in Java ArrayList The minimum value is the one with the smallest value and the maximum value is the one with the largest value. The main task here is to find the minimum and maximum value from the ArrayList. Consider an example of an ArrayList, and we need to find the largest and the smallest element. Example: Input
5 min read
Sort LinkedHashMap by Values using Comparable Interface in Java The LinkedHashMap is just like HashMap with an additional feature of maintaining an order of elements inserted into it. Assuming you have gone through LinkedHashMap in java and know about LinkedHashMap. Syntax: int compare(T obj) ; Illustration: Input : { GEEKS=1, geeks=3, for=2 } Output : { GEEKS=1
2 min read
Searching Elements in Vector Using Index in Java 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. An element of a Vector can be searched using an index with different methods. Suppose if the element is not present in the Vector then th
4 min read
Java Program to Find maximum element of each row in a matrix Given a matrix, the task is to find the maximum element of each row. Examples: Input : [1, 2, 3] [1, 4, 9] [76, 34, 21] Output : 3 9 76 Input : [1, 2, 3, 21] [12, 1, 65, 9] [1, 56, 34, 2] Output : 21 65 56 Approach : Approach is very simple. The idea is to run the loop for no_of_rows. Check each ele
2 min read
Finding Maximum Element of Java ArrayList For finding the maximum element in the ArrayList, complete traversal of the ArrayList is required. There is an inbuilt function in the ArrayList class to find the maximum element in the ArrayList, i.e. Time Complexity is O(N), where N is the size of ArrayList, Let's discuss both the methods. Example
2 min read
Sort Java Vector in Descending Order Using Comparator The Vector class implements a growable array of objects. Vectors basically fall in legacy classes but now it is fully compatible with collections. It is found in java.util package and implements the List interface, so we can use all the methods of the List interface. There are two types of Sorting t
3 min read