对具有共同实例变量的各种对象的 ArrayList 进行排序
Sorting an ArrayList of various objects having in common instace variable being a double
我最近一直在研究接口,并且能够成功实现 Comparable
并按数量对 ArrayList
个 BankAccount
个对象进行排序。
但是,如果 ArrayList
包含 BankAccount
和 Country
类型的对象,并且具有实例变量 "area"(双精度)。它们都具有 double 类型的值,可以进行比较和排序。
所以我的第一个问题是——
如何对包含具有 double 作为实例变量的各种对象的 ArrayList 进行排序?
这是我的代码:
/**
A bank account has a balance that can be changed by
deposits and withdrawals.
*/
public class BankAccount implements Measurable, Comparable<BankAccount>
{
private Double balance;
/**
Constructs a bank account with a zero balance.
*/
public BankAccount()
{
balance = 0.0;
}
/**
Constructs a bank account with a given balance.
@param initialBalance the initial balance
*/
public BankAccount(Double initialBalance)
{
balance = initialBalance;
}
/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
public void deposit(Double amount)
{
balance = balance + amount;
}
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(Double amount)
{
balance = balance - amount;
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
public double getMeasure()
{
return balance;
}
public int compareTo(BankAccount o)
{
return this.balance.compareTo(o.getBalance());
}
}
/**
A country with a name and area.
*/
public class Country implements Measurable
{
private String name;
private double area;
/**
Constructs a country.
@param aName the name of the country
@param anArea the area of the country
*/
public Country(String aName, double anArea)
{
name = aName;
area = anArea;
}
/**
Gets the country name.
@return the name
*/
public String getName()
{
return name;
}
/**
Gets the area of the country.
@return the area
*/
public double getArea()
{
return area;
}
public double getMeasure()
{
return area;
}
}
public interface Measurable
{
double getMeasure(); // An abstract method
}
import java.util.*;
public class MeasurableTester
{
public static void main(String[] args)
{
// Calling the average method with an array of BankAccount objects
BankAccount b=new BankAccount(10.0);
BankAccount c = new BankAccount(2000.0);
List<BankAccount> accounts = new ArrayList<BankAccount>();
accounts.add(b);
accounts.add(c);
BankAccount x= Collections.max(accounts);
System.out.println(x.getBalance());
}
}
您应该使用可测量列表并使用比较器:
List<Measurable> measurableList = new ArrayList<>();
measurableList.add(new BankAccount(10.0));
measurableList.add(new Country("name", 44.5));
measurableList = Collections.max(measurableList, Comparator.comparingDouble(Measurable::getMeasure));
我最近一直在研究接口,并且能够成功实现 Comparable
并按数量对 ArrayList
个 BankAccount
个对象进行排序。
但是,如果 ArrayList
包含 BankAccount
和 Country
类型的对象,并且具有实例变量 "area"(双精度)。它们都具有 double 类型的值,可以进行比较和排序。
所以我的第一个问题是—— 如何对包含具有 double 作为实例变量的各种对象的 ArrayList 进行排序?
这是我的代码:
/**
A bank account has a balance that can be changed by
deposits and withdrawals.
*/
public class BankAccount implements Measurable, Comparable<BankAccount>
{
private Double balance;
/**
Constructs a bank account with a zero balance.
*/
public BankAccount()
{
balance = 0.0;
}
/**
Constructs a bank account with a given balance.
@param initialBalance the initial balance
*/
public BankAccount(Double initialBalance)
{
balance = initialBalance;
}
/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
public void deposit(Double amount)
{
balance = balance + amount;
}
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(Double amount)
{
balance = balance - amount;
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
public double getMeasure()
{
return balance;
}
public int compareTo(BankAccount o)
{
return this.balance.compareTo(o.getBalance());
}
}
/**
A country with a name and area.
*/
public class Country implements Measurable
{
private String name;
private double area;
/**
Constructs a country.
@param aName the name of the country
@param anArea the area of the country
*/
public Country(String aName, double anArea)
{
name = aName;
area = anArea;
}
/**
Gets the country name.
@return the name
*/
public String getName()
{
return name;
}
/**
Gets the area of the country.
@return the area
*/
public double getArea()
{
return area;
}
public double getMeasure()
{
return area;
}
}
public interface Measurable
{
double getMeasure(); // An abstract method
}
import java.util.*;
public class MeasurableTester
{
public static void main(String[] args)
{
// Calling the average method with an array of BankAccount objects
BankAccount b=new BankAccount(10.0);
BankAccount c = new BankAccount(2000.0);
List<BankAccount> accounts = new ArrayList<BankAccount>();
accounts.add(b);
accounts.add(c);
BankAccount x= Collections.max(accounts);
System.out.println(x.getBalance());
}
}
您应该使用可测量列表并使用比较器:
List<Measurable> measurableList = new ArrayList<>();
measurableList.add(new BankAccount(10.0));
measurableList.add(new Country("name", 44.5));
measurableList = Collections.max(measurableList, Comparator.comparingDouble(Measurable::getMeasure));