简体   繁体   English

如何将对象添加到对象哈希图中的排序集中

[英]How to add a object to a sorted set that's within a hashmap of objects

I'm trying to add a passenger object into a sorted set. 我正在尝试将乘客对象添加到排序集中。 This sorted set is in a cruise object. 该排序的集合位于巡航对象中。 All of the cruise objects are within a hashMap. 所有的巡航对象都在hashMap中。 I'm kinda new to collections so I'm having trouble. 我是一个新手,所以遇到了麻烦。 This is my attempt to do what I'm doing. 这是我尝试做的事情。

HashMap<String, Cruise> cruiseMap = new HashMap<String, Cruise>();
SortedSet<Passenger> passengerSet = new TreeSet<Passenger>();
Queue<Passenger> waitingList = new LinkedList<Passenger>();

Cruise cruise = new Cruise("1", passengerSet, waitingList, false);

cruiseMap.put("1", cruise);
Passenger passenger = new Passenger("Smith", "J");
cruiseMap.get("1").getPassengerSet().add(passenger);

The passenger's parameters are strings that are last name then their first initial. 乘客的参数是字符串,字符串是姓氏,然后是他们的第一个名字首字母。 The cruise's parameters are as a string the date, the sortedSet passengers, there's a queue for waiting list and a boolean variable to determine if the ship has departed. 巡游的参数是日期字符串,sortedSet乘客,等待队列的队列和布尔值变量,以确定船只是否已出发。 I keep getting tons of errors when I run this code. 运行此代码时,我不断收到大量错误。 Thanks in advance for the help. 先谢谢您的帮助。

Here are the errors I'm recieving. 这是我收到的错误。

Exception in thread "main" java.lang.ClassCastException: edu.ilstu.Passenger cannot be cast to java.lang.Comparable
at java.util.TreeMap.compare(Unknown Source)
at java.util.TreeMap.put(Unknown Source)
at java.util.TreeSet.add(Unknown Source)
at edu.ilstu.Driver.main(Driver.java:48)

Passenger Class 旅客舱位

public class Passenger {
    private String lastName = "";
    private String firstName = "";

    public Passenger()
    {
        lastName = "no last name yet";
        firstName = "no first name yet";
    }
    public Passenger(String lastName, String firstName)
    {
        this.lastName = lastName;
        this.firstName = firstName;
    }
    /**
     * @return the lastName
     */
    public String getLastName()
    {
        return lastName;
    }
    /**
     * @param lastName the lastName to set
     */
    public void setLastName(String lastName)
    {
    this.lastName = lastName;
    }
    /**
     * @return the firstName
     */
    public String getFirstName()
    {
        return firstName;
    }
    /**
     * @param firstName the firstName to set
     */
    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString()
    {
        return lastName + " " + firstName;
    }
}

Cruise Class 邮轮班

public class Cruise
    {
    private String day = "";
    private  SortedSet<Passenger> passengerSet = new TreeSet<Passenger>();
    private Queue<Passenger> waitingList = new LinkedList<Passenger>();
    private boolean hasDeparted = false;

    public Cruise()
    {
        day = "no day yet";
        passengerSet = null;
        waitingList = null;
        hasDeparted = false;
    }

    public Cruise(String day, SortedSet<Passenger> passengerSet,     Queue<Passenger> waitingList, boolean hasDeparted)
    {
        this.day = day;
        this.passengerSet = passengerSet;
        this.waitingList = waitingList;
        this.hasDeparted = hasDeparted;
    }

    /**
     * @return the day
     */
    public String getDay()
    {
        return day;
    }

    /**
     * @param day the day to set
     */
    public void setDay(String day)
    {
        this.day = day;
    }

    /**
     * @return the passengerSet
     */
    public SortedSet<Passenger> getPassengerSet()
    {
        return passengerSet;
    }

    /**
     * @param passengerSet the passengerSet to set
     */
    public void setPassengerSet(SortedSet<Passenger> passengerSet)
    {
        this.passengerSet = passengerSet;
    }

    /**
     * @return the waitingList
     */
    public Queue<Passenger> getWaitingList()
    {
        return waitingList;
    }

    /**
     * @param waitingList the waitingList to set
     */
    public void setWaitingList(Queue<Passenger> waitingList)
    {
        this.waitingList = waitingList;
    }

    /**
     * @return the hasDeparted
     */
    public boolean isHasDeparted()
    {
        return hasDeparted;
    }

    /**
     * @param hasDeparted the hasDeparted to set
     */
    public void setHasDeparted(boolean hasDeparted)
    {
        this.hasDeparted = hasDeparted;
    }

}

It happens because your passengerSet is TreeSet ( SortedSet ), which means it will sort itself after each adding, because TreeSet is ordered set and has certain sequence unlike usual HashMap . 发生这种情况的原因是您的passengerSetTreeSetSortedSet ),这意味着它将在每次添加后对其进行排序,因为TreeSet是有序集合,并且具有不同于常规HashMap特定顺序。 Every SortedMap must know how to sort elements it contains. 每个SortedMap必须知道如何对它包含的元素进行排序。 This can be done two ways: 这可以通过两种方式完成:

  1. You can implement your class from Comparable<T> interface. 您可以从Comparable<T>接口实现您的类。
  2. You can add custom Comparator<T> to your SortedMap . 您可以将自定义Comparator<T>添加到SortedMap

So, you have three ways to fix it (may be more, but three of them - are obvious): 因此,您可以通过三种方式进行修复(可能更多,但其中三种很明显):

  1. Get rid of SortedMap , let's say replace your SortedMap to Map and replace TreeMap to HashMap in your code. 摆脱SortedMap ,假设在代码中将SortedMap替换为Map并将TreeMap替换为HashMap
  2. Add custom comparator to your passengerSet 将自定义比较器添加到您的passengerSet

     HashMap<String, Cruise> cruiseMap = new HashMap<String, Cruise>(); SortedSet<Passenger> passengerSet = new TreeSet<Passenger>(new Comparator<Passenger>() { @Override public int compare(Passenger lhs, Passenger rhs) { return lhs.getFirstName().compareTo(rhs.getFirstName()); } }); Queue<Passenger> waitingList = new LinkedList<>(); Cruise cruise = new Cruise("1", passengerSet, waitingList, false); cruiseMap.put("1", cruise); Passenger passenger = new Passenger("Smith", "J"); cruiseMap.get("1").getPassengerSet().add(passenger); 
  3. Implement the Comparable<T> interface in your Passenger class. Passenger类中实现Comparable<T>接口。

     public class Passenger implements Comparable<Passenger> { private String lastName = ""; private String firstName = ""; public Passenger() { lastName = "no last name yet"; firstName = "no first name yet"; } public Passenger(String lastName, String firstName) { this.lastName = lastName; this.firstName = firstName; } /** * @return the lastName */ public String getLastName() { return lastName; } /** * @param lastName the lastName to set */ public void setLastName(String lastName) { this.lastName = lastName; } /** * @return the firstName */ public String getFirstName() { return firstName; } /** * @param firstName the firstName to set */ public void setFirstName(String firstName) { this.firstName = firstName; } /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return lastName + " " + firstName; } @Override public int compareTo(Passenger another) { return firstName.compareTo(another.firstName); } } 

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

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