HashMap.get(linkedList) 引发的 ClassCastException
ClassCastException being thrown as a result of HashMap.get(linkedList)
处理 this 和类似的条目我创建了这段代码来从 LinkedList 的 HashMap 中删除一个条目。
//Dbase in question
HashMap<String, LinkedList<Item>> authorDbase = new HashMap<String, LinkedList<Item>>();
TreeMap<String, Item> bookAisle = new TreeMap<String, Item>();
...
Item book = bookAisle.get(title); // book == title's Item object reference
authorDbase.get(author).remove(book); //Removes the item mapped to specified keyword
当在 main 中调用它所属的函数时,出现以下错误。
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to library.Item
at library.Item.compareTo(Item.java:8)
at java.util.TreeMap.getEntry(TreeMap.java:352)
at java.util.TreeMap.remove(TreeMap.java:603)
at library.Library.removeBook(Library.java:287)
at Assignment3.removeBook(Assignment3.java:134)
at Assignment3.main(Assignment3.java:118)
我是这样实现的 Comparable<Item>
8 public abstract class Item implements Comparable<Item>
9 {
...
26 @Override
27 public int compareTo(Item i)
28 {
29 // String title = this.title; DEBUG FLAG: delete maybe?
30 return this.title.compareTo(i.title); //Returns a negative value if title < i.title, implements alphabetical order by title
31 }
我的删除算法有什么问题?我知道它试图将 compareTo
与不正确的参数一起使用,这就是抛出异常的原因,但我不应该能够从 HashMap.get(listWanted)
的结果中调用 LinkedList<Item>
的任何函数?为什么 Comparable
在这里是一个因素?有人可以建议修复并纠正我对此的理解吗?
编辑
118行只是调用了删除算法所在的remove函数
118 removeBook(out, "The Curious Incident of the Dog in the Night-Time");
这里是removeBook
279 public boolean removeBook(String title)
280 {
281 Item book = bookAisle.get(title); // book == title's Item object reference
282 String author;
283 boolean successFlag = false;
284
285 if(book == null) //title doesn't exist in the bookAisle
286 successFlag = false;
287 else if (bookAisle.remove(book) != null) //It does exist and was removed
288 {
289 //Deletes from author dbase
290 author = book.getCreator(); //placed here to avoid potentially calling on a null object
291 authorDbase.get(author).remove(book); //Removes the item mapped to specified keyword
292 //Deletes from keyword dbase
293 removeFromKDbase(book); //Removes object reference from KDbase
294 successFlag = true;
295 }
296
297 return successFlag;
298 }
在第 287 行中,您正在调用 bookAisle.remove(book),它有效地尝试从确实包含 String
作为键的 Treemap 中删除 Item
。
(树形图的 Javadoc:"throws java.lang.ClassCastException if the specified key cannot be compared with the keys currently in the map"。)
如果将行更改为 bookAisle.remove(title)
,那么它应该可以工作。
处理 this 和类似的条目我创建了这段代码来从 LinkedList 的 HashMap 中删除一个条目。
//Dbase in question
HashMap<String, LinkedList<Item>> authorDbase = new HashMap<String, LinkedList<Item>>();
TreeMap<String, Item> bookAisle = new TreeMap<String, Item>();
...
Item book = bookAisle.get(title); // book == title's Item object reference
authorDbase.get(author).remove(book); //Removes the item mapped to specified keyword
当在 main 中调用它所属的函数时,出现以下错误。
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to library.Item
at library.Item.compareTo(Item.java:8)
at java.util.TreeMap.getEntry(TreeMap.java:352)
at java.util.TreeMap.remove(TreeMap.java:603)
at library.Library.removeBook(Library.java:287)
at Assignment3.removeBook(Assignment3.java:134)
at Assignment3.main(Assignment3.java:118)
我是这样实现的 Comparable<Item>
8 public abstract class Item implements Comparable<Item>
9 {
...
26 @Override
27 public int compareTo(Item i)
28 {
29 // String title = this.title; DEBUG FLAG: delete maybe?
30 return this.title.compareTo(i.title); //Returns a negative value if title < i.title, implements alphabetical order by title
31 }
我的删除算法有什么问题?我知道它试图将 compareTo
与不正确的参数一起使用,这就是抛出异常的原因,但我不应该能够从 HashMap.get(listWanted)
的结果中调用 LinkedList<Item>
的任何函数?为什么 Comparable
在这里是一个因素?有人可以建议修复并纠正我对此的理解吗?
编辑
118行只是调用了删除算法所在的remove函数
118 removeBook(out, "The Curious Incident of the Dog in the Night-Time");
这里是removeBook
279 public boolean removeBook(String title)
280 {
281 Item book = bookAisle.get(title); // book == title's Item object reference
282 String author;
283 boolean successFlag = false;
284
285 if(book == null) //title doesn't exist in the bookAisle
286 successFlag = false;
287 else if (bookAisle.remove(book) != null) //It does exist and was removed
288 {
289 //Deletes from author dbase
290 author = book.getCreator(); //placed here to avoid potentially calling on a null object
291 authorDbase.get(author).remove(book); //Removes the item mapped to specified keyword
292 //Deletes from keyword dbase
293 removeFromKDbase(book); //Removes object reference from KDbase
294 successFlag = true;
295 }
296
297 return successFlag;
298 }
在第 287 行中,您正在调用 bookAisle.remove(book),它有效地尝试从确实包含 String
作为键的 Treemap 中删除 Item
。
(树形图的 Javadoc:"throws java.lang.ClassCastException if the specified key cannot be compared with the keys currently in the map"。)
如果将行更改为 bookAisle.remove(title)
,那么它应该可以工作。