【问题标题】:How to add a object to a sorted set that's within a hashmap of objects如何将对象添加到对象哈希图中的排序集中
【发布时间】:2015-04-05 19:44:12
【问题描述】:

我正在尝试将乘客对象添加到排序集中。该排序集位于巡航对象中。所有的 Cruise 对象都在一个 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;
    }

}

【问题讨论】:

  • 请将实际错误添加到您的问题中。
  • 请添加 Cruise 和 Passenger 类。
  • 信息已添加。
  • 可能是this?
  • 那个链接帮我弄明白了!谢谢。我需要在乘客类中实现 Comparable。谢谢。

标签: java object collections hashmap sortedset


【解决方案1】:

这是因为您的passengerSetTreeSet (SortedSet),这意味着它会在每次添加后自行排序,因为TreeSet 是有序集合并且具有与通常的HashMap 不同的特定顺序。每个SortedMap 都必须知道如何对其包含的元素进行排序。这可以通过两种方式完成:

  1. 您可以从Comparable&lt;T&gt; 接口实现您的类。
  2. 您可以将自定义Comparator&lt;T&gt; 添加到您的SortedMap

所以,你有三种方法来解决它(可能更多,但其中三种 - 很明显):

  1. 摆脱SortedMap,假设在您的代码中将SortedMap 替换为Map 并将TreeMap 替换为HashMap
  2. 将自定义比较器添加到您的 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. 在您的Passenger 类中实现Comparable&lt;T&gt; 接口。

    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);
        }
    }
    

【讨论】:

    猜你喜欢
    • 2017-09-20
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    • 2017-08-20
    • 2016-11-18
    • 1970-01-01
    • 2019-01-18
    • 2022-12-11
    相关资源
    最近更新 更多