简体   繁体   English

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

[英]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. 我有一个平面的哈希图,我想比较它们,以便可以通过按timeLimitBeforeLand对地图进行排序来获取下一平面和最后一个平面。 I wrote a compareTo method which looks like : 我写了一个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 takes timeLimitBeforeLand: 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;
        }

My attempt so far in the mainApp: 到目前为止,我在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 . Collections.sort的第二个参数是针对Comparator而不是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 由于我没有提到Comparator,因此您应该能够使用自然顺序(由Plane对象中的compareTo方法定义),并且在Collections.sort没有第二个参数。

EDIT : Unless you have just excluded that code, you aren't creating any Plane instances and you're using empty collections here... 编辑 :除非您只是排除了该代码,否则您不会创建任何Plane实例,并且在这里使用空集合...

   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. 并且您将按PlaneStores进行排序,因此您必须在每个PlaneStore中获取所有Plane ,然后将它们添加到您的copyList然后再进行排序。

I would consider researching each of the Collections a little more and deciding what the best one for your need would be. 我会考虑对每个系列进行更多的研究,然后确定最适合您的系列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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