[英]How to add a object to a sorted set that's within a hashmap of objects
我正在嘗試將乘客對象添加到排序集中。 該排序的集合位於巡航對象中。 所有的巡航對象都在hashMap中。 我是一個新手,所以遇到了麻煩。 這是我嘗試做的事情。
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);
乘客的參數是字符串,字符串是姓氏,然后是他們的第一個名字首字母。 巡游的參數是日期字符串,sortedSet乘客,等待隊列的隊列和布爾值變量,以確定船只是否已出發。 運行此代碼時,我不斷收到大量錯誤。 先謝謝您的幫助。
這是我收到的錯誤。
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)
旅客艙位
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;
}
}
郵輪班
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;
}
}
發生這種情況的原因是您的passengerSet
是TreeSet
( SortedSet
),這意味着它將在每次添加后對其進行排序,因為TreeSet
是有序集合,並且具有不同於常規HashMap
特定順序。 每個SortedMap
必須知道如何對它包含的元素進行排序。 這可以通過兩種方式完成:
Comparable<T>
接口實現您的類。 Comparator<T>
添加到SortedMap
。 因此,您可以通過三種方式進行修復(可能更多,但其中三種很明顯):
SortedMap
,假設在代碼中將SortedMap
替換為Map
並將TreeMap
替換為HashMap
。 將自定義比較器添加到您的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);
在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.