Search code examples
javacomparable

Sorting an ArrayList of various objects having in common instace variable being a double


I've been playing around with interfaces lately and was able to successfully implement Comparable and sort an ArrayList of BankAccount objects by their amount.

But what if the ArrayList contained objects of type BankAccount and Country having instance variable "area" (double). They both have value of type double which can be compared and sorted.

So my first question is- How to sort ArrayList containing various objects having a double as instance variable?

Here is my code:

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


}

Solution

  • You should use List of Measurables and use comparator:

      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));