Java 使用可比较接口查找矢量的最小和最大元素

Java 使用可比较接口查找矢量的最小和最大元素

java中的Vector类实现了一个动态数组,也就是说,它可以根据我们插入或移除的元素而增长或缩小。它实现了List接口,所以它支持List接口所提供的所有方法。

在这篇文章中,我们将讨论如何使用Java中的Comparable接口找到一个向量的最小和最大元素。

我们可以使用Collections.min()和Collections.max()方法来查找Vector中的最小和最大元素。但是当我们使用我们的自定义类并想使用Collections.min()和Collections.max()方法时,我们首先需要覆盖Comparable接口的compareTo()方法,这样java就能够比较我们自定义类的实例。另外,由于我们要比较的是对象,而不是原始数据类型,所以我们必须使用比较接口中自定义的compareTo()方法。

**实现可比较 接口的语法。 **

class Account implements Comparable<Account>
Java

使用上述语法,我们可以为我们的自定义类,即帐户类,实现比较接口。

覆盖CompareTo()方法的语法。

@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;
}
Java

上面的compareTo()方法根据余额属性对账户对象进行比较。我们的compareTo()方法必须返回。

  • 零:当两个对象相等时。
  • 负值:当当前对象小于传递的对象时,即’o’。
  • 正值:当当前对象大于传递对象时。

下面是使用可比接口找到矢量的最小和最大元素的代码实现。

// 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());
    }
}
Java

输出

Nandini has the minimum balance of Rs 50
Pooja has the maximum balance of Rs 50000
Java

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册