简体   繁体   English

不能转换为 java.lang.Comparable

[英]cannot be cast to java.lang.Comparable

import java.util.HashMap;
import java.io.*;
import java.util.*;
public class Fegan implements Comparable{
HashMap<String, Integer> cart = new HashMap<String, Integer>();
List list = new ArrayList<FoodItems>();
int x =0;
public void addToCart(FoodItems f)
{
    cart.put(f.name, f.valueOfFood);
}
public String display(FoodItems f)
{
    return(f.name + " costs " + f.valueOfFood);
}
public void addToList(FoodItems f)
{
    //FoodItems temp = (FoodItems) f;
    list.add(f);
}
public int compareTo(Object o)
{
    //FoodItems temp = (FoodItems) o;
    if(this.x == ((FoodItems)o).valueOfFood)
        return 0;
    else if (this.x <((FoodItems)o).valueOfFood)
        return 1;
    else 
        return -1;
}
public void sortMap(List list)
{
    for(int i =0; i< list.size(); i++)
    {
        FoodItems temp = (FoodItems) list.get(i);
        cart.put(temp.name, temp.valueOfItem);

    }
}
}

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.io.*;
import java.util.*;
public class Test {
public static void main(String [] args)
{
    HashMap<String, Integer> cart = new HashMap<String, Integer>();
    FoodItems firts = new FoodItems("Chocolate" , 50);
    FoodItems second = new FoodItems("Juice", 79);
    FoodItems third = new FoodItems("Apple", 200);
    FoodItems forth = new FoodItems("Orange", 300);
    FoodItems fifth = new FoodItems("Milk" , 400);
    ArrayList items = new ArrayList();
    items.add(firts);
    items.add(second);
    items.add(third);
    items.add(forth);
    items.add(fifth);
    Collections.sort(items);
    Iterator itr = items.iterator();
    Fegan myFegan = new Fegan();
    myFegan.sortMap(items);

    while(itr.hasNext()){
        Object element = itr.next();
        System.out.println(element + "\n");
    }
}
}

Why it is writing:为什么这么写:

Exception in thread "main" java.lang.ClassCastException: FoodItems cannot be cast to java.lang.Comparable
at java.util.ComparableTimSort.countRunAndMakeAscending(Unknown Source)
at java.util.ComparableTimSort.sort(Unknown Source)
at java.util.ComparableTimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at java.util.Collections.sort(Unknown Source)
at Test.main(Test.java:21)
  • the object which implements Comparable is Fegan .实现Comparable的对象是Fegan

The method compareTo you are overidding in it should have a Fegan object as a parameter whereas you are casting it to a FoodItems .您在其中覆盖的方法compareTo应该有一个Fegan对象作为参数,而您将其转换为FoodItems Your compareTo implementation should describe how a Fegan compare to another Fegan .您的compareTo实现应该描述Fegan与另一个Fegan比较。

  • To actually do your sorting, you might want to make your FoodItems implement Comparable aswell and copy paste your actual compareTo logic in it.要实际进行排序,您可能还想让FoodItems实现Comparable并将实际的compareTo逻辑复制粘贴到其中。

I faced a similar kind of issue while using a custom object as a key in Treemap.在使用自定义对象作为 Treemap 中的键时,我遇到了类似的问题。 Whenever you are using a custom object as a key in hashmap then you override two function equals and hashcode, However if you are using ContainsKey method of Treemap on this object then you need to override CompareTo method as well otherwise you will be getting this error Someone using a custom object as a key in hashmap in kotlin should do like following每当您使用自定义对象作为 hashmap 中的键时,您就会覆盖两个函数 equals 和 hashcode,但是,如果您在此对象上使用 Treemap 的 ContainsKey 方法,则您还需要覆盖 CompareTo 方法,否则您将收到此错误在 kotlin 中使用自定义对象作为 hashmap 中的键应该如下所示

 data class CustomObjectKey(var key1:String = "" , var 
 key2:String = ""):Comparable<CustomObjectKey?>
 {
override fun compareTo(other: CustomObjectKey?): Int {
    if(other == null)
        return -1
   // suppose you want to do comparison based on key 1 
    return this.key1.compareTo((other)key1)
}

override fun equals(other: Any?): Boolean {
    if(other == null)
        return false
    return this.key1 == (other as CustomObjectKey).key1
}

override fun hashCode(): Int {
    return this.key1.hashCode()
} 
}

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

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