简体   繁体   中英

How to use CompareTo to sort the PlaneMap by Ascending and Descending order

Im trying to sort my planes by Ascending and Descending order. I have a hashmap of planes and i want to compare them so that i can get the next plane due and last plane due by sorting the map by timeLimitBeforeLand. I wrote a compareTo method which looks like :

//---------------------------------------------------------------------------------------
//  CompareTo() used with the Comparable implementation.
//---------------------------------------------------------------------------------------
        public int compareTo(Object arg0) 
        {
            if((arg0 != null) && (arg0 instanceof Plane))
            {
            Plane p = (Plane) arg0;
            return (int)Math.ceil(this.timeLimitBeforeLand - p.getLimitBeforeLand());
            }
            return 0;
        }

CompareTo takes timeLimitBeforeLand:

// ---------------------------------------------------------------------------------------
//      Name:        getTimeLimitBeforeLand.
//      Description: Get the time before every plane is going to land.
//---------------------------------------------------------------------------------------
        public double getTimeLimitBeforeLand()
        {
        double fuelConsumption;
        double timeLimitBeforeLand = 0;

        for (TreeMap<String, Plane> theEntry : airlineMap.values()) {
        for (Plane aPlane : theEntry.values()) {
        if (aPlane.getPlaneType() == aPlane.getPlaneType().AIRBUS) {
        System.out.println(" ");
        System.out.println(aPlane);
        fuelConsumption = 2;
        timeLimitBeforeLand = (double) (aPlane.getFuelRemaining() / fuelConsumption);
        System.out.println(timeLimitBeforeLand + " minutes to land.");
        System.out.println(" ");
        } else if (aPlane.getPlaneType() == aPlane.getPlaneType().CORPORATE) {
        System.out.println(" ");
        System.out.println(aPlane);
        fuelConsumption = 3;
        timeLimitBeforeLand = (aPlane.getFuelRemaining() / fuelConsumption);
        System.out.println(timeLimitBeforeLand + " minutes to land.");
        System.out.println(" ");
        } else if (aPlane.getPlaneType() == aPlane.getPlaneType().PRIVATE) {
        System.out.println(" ");
        System.out.println(aPlane);
        fuelConsumption = 4;
        timeLimitBeforeLand = (double) (aPlane.getFuelRemaining() / fuelConsumption);
        System.out.println(timeLimitBeforeLand + " minutes to land.");
        System.out.println(" ");
        }
        }
        }
        return timeLimitBeforeLand;
        }

My attempt so far in the mainApp:

TreeMap<String, PlaneStore> map = new TreeMap<String, PlaneStore>();

        ArrayList<Plane> copyList = new ArrayList<Plane>(map.);

        Plane comp = new Plane();

        Collections.sort(copyList, plane);

Plane Class:

//---------------------------------------------------------------------------------------
//  Name:           Imports. 
//  Description:    To allow the use of different Java classes.
//---------------------------------------------------------------------------------------
import java.io.Serializable;
//---------------------------------------------------------------------------------------
//Name:         Class declaration. 
//---------------------------------------------------------------------------------------
public class Plane implements Comparable, Serializable
{   
//---------------------------------------------------------------------------------------
//  Variable declarations.
//---------------------------------------------------------------------------------------
    private String flightNumber;
    public String airlineName;
    private double fuelRemaining;
    private int overdue;
    private int passengerNumber;
//---------------------------------------------------------------------------------------
//  Enum declaration.
//---------------------------------------------------------------------------------------
    private AIRPLANETYPE planeType;
    private boolean isLanded = false;
    public double timeLimitBeforeLand;
//---------------------------------------------------------------------------------------
//  Enum Constuctor.
//---------------------------------------------------------------------------------------
    public enum AIRPLANETYPE
    {
        AIRBUS("1"), CORPORATE("2"), PRIVATE("3");

        private String planeName;

        private AIRPLANETYPE(String planeName)
        {
            this.planeName = planeName;
        }

        public String getPlaneName()
        {
            return this.planeName;
        }
    }
//---------------------------------------------------------------------------------------
//  Constructor.
//---------------------------------------------------------------------------------------
    public Plane(String flightNumber, String airlineName,
           double fuelRemaining, int overdue, int passengerNumber, 
           AIRPLANETYPE planeType, boolean isLanded) 
    {
        this.flightNumber = flightNumber;
        this.airlineName = airlineName;
        this.fuelRemaining = fuelRemaining;
        this.passengerNumber = passengerNumber;
        this.overdue = overdue;
        this.planeType = planeType;
        this.isLanded = isLanded;
    }
//---------------------------------------------------------------------------------------
//  Getters and Setters.
//---------------------------------------------------------------------------------------
    public String getAirlineName() 
    {
        return airlineName;
    }
    public void setAirlineName(String airlineName) 
    {
        this.airlineName = airlineName;
    }
    public void setOverdue(int overdue) 
    {
        this.overdue = overdue;
    }
    public int getOverdue()
    {
        return overdue;
    }
    public String getFlightNumber() 
    {
        return flightNumber;
    }
    public void setFlightNumber(String flightNumber) 
    {
        this.flightNumber = flightNumber;
    }
    public double getFuelRemaining() 
    {
        return fuelRemaining;
    }
    public void setFuelRemaining(double fuelRemaining) 
    {
        this.fuelRemaining = fuelRemaining;
    }
    public int getPassengerNumber() 
    {
        return passengerNumber;
    }
    public void setPassengerNumber(int passengerNumber) 
    {
        this.passengerNumber = passengerNumber;
    }
    public AIRPLANETYPE getPlaneType() 
    {
        return planeType;
    }
    public void setPlaneType(AIRPLANETYPE planeType) 
    {
        this.planeType = planeType;
    }
    public boolean isLanded() 
    {
        return isLanded;
    }
    public void setLanded(boolean isLanded)
    {
        this.isLanded = isLanded;
    }
    public double getLimitBeforeLand() 
    {
        return timeLimitBeforeLand;
    }

public void setTimeLimitBeforeLand(double timeLimitBeforeLand) 
{
this.timeLimitBeforeLand = timeLimitBeforeLand;
}

//---------------------------------------------------------------------------------------
//  CompareTo() used with the Comparable implementation.
//---------------------------------------------------------------------------------------
    public int compareTo(Object arg0) 
    {
        if((arg0 != null) && (arg0 instanceof Plane))
        {
        Plane p = (Plane) arg0;
        return (int)Math.ceil(this.timeLimitBeforeLand - p.getLimitBeforeLand());
        }
        return 0;
    }
//---------------------------------------------------------------------------------------
//  toString().
//---------------------------------------------------------------------------------------
    public String toString() 
    {
        return "Plane: flightNumber=" + flightNumber + "."
                + " airlineName=" + airlineName + "."
                + " fuelRemaining=" + fuelRemaining + " litres."
                + " overdue=" + overdue + " minutes."
                + " passengerNumber="+ passengerNumber + "."
                + " airplaneType=" + planeType +
                "hasLanded=" + isLanded+ ".\n";
    }
}

The second argument in Collections.sort is for a Comparator not a Plane . Since I saw no mention of a Comparator, you should be able to use the natural order (defined by the compareTo method in your Plane object) and not have a second argument in the Collections.sort

EDIT : Unless you have just excluded that code, you aren't creating any Plane instances and you're using empty collections here...

   TreeMap<String, PlaneStore> map = new TreeMap<String, PlaneStore>();
   ArrayList<Plane> copyList = new ArrayList<Plane>(map.);

and you will be sorting by PlaneStores so you have to obtain all the Plane s in each PlaneStore and add them to your copyList before sorting.

I would consider researching each of the Collections a little more and deciding what the best one for your need would be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM