如何使用CompareTo按升序和降序对PlaneMap进行排序

时间:2012-12-14 17:54:39

标签: java hashmap compareto

我试图按升序和降序对我的飞机进行排序。我有一个飞机的散列图,我想比较它们,以便我可以通过timeLimitBeforeLand对地图进行排序,从而得到下一架飞机到期和最后一架飞机。我写了一个compareTo方法,它看起来像:

//---------------------------------------------------------------------------------------
//  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需要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;
        }

我在mainApp中的尝试:

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

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

        Plane comp = new Plane();

        Collections.sort(copyList, plane);

飞机等级:

//---------------------------------------------------------------------------------------
//  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";
    }
}

1 个答案:

答案 0 :(得分:0)

Collections.sort中的第二个参数是Comparator而不是Plane。由于我没有提到比较器,你应该能够使用自然顺序(由Plane对象中的compareTo方法定义)而不是Collections.sort

中的第二个参数

编辑:除非您刚刚排除了该代码,否则您无法创建任何平面实例,并且您在此处使用空集合...

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

您将按PlaneStores排序,因此您必须获取每个PlaneStore中的所有Plane并在排序前将它们添加到copyList

我会考虑更多地研究Collections中的每一项,并根据您的需要决定哪种最佳。