简体   繁体   English

MyClass无法强制转换为java.lang.Comparable:java.lang.ClassCastException

[英]MyClass cannot be cast to java.lang.Comparable: java.lang.ClassCastException

I am doing a java project and I got this problem and don't know how to fix it. 我正在做一个java项目,我遇到了这个问题,不知道如何解决它。

The classes in my project (simplified): 我项目中的类(简化):

public class Item {

    private String itemID;
    private Integer price;

    public Integer getPrice() {

        return this.price;

    }

}

public class Store {

    private String storeID;
    private String address;

}

public class Stock {

    private Item item;
    private Store store;
    private Integer itemCount;

    public Integer getInventoryValue() {

        return this.item.getPrice() * this.itemCount;

    }  
}

Then I try to sort an ArrayList of Stock so I create another class called CompareByValue 然后我尝试排序Stock的ArrayList ,所以我创建了另一个名为CompareByValue

public class CompareByValue implements Comparator<Stock> {

    @Override
    public int compare(Stock stock1, Stock stock2) {

        return (stock1.getInventoryValue() - stock2.getInventoryValue());

    }

} }

When I try to run the program, it gives the error: 当我尝试运行该程序时,它会给出错误:

Exception in thread "main" java.lang.ClassCastException: Stock cannot be cast to java.lang.Comparable 线程“main”中的异常java.lang.ClassCastException:Stock无法强制转换为java.lang.Comparable

Anyone know what's wrong? 谁知道什么是错的?

It's because Stock isn't implementing Comparable . 这是因为Stock没有实现Comparable Either implement it: 实现它:

public class Stock implements Comparable<Stock> {
    public int compareTo(Stock o) {
        // ...
    }
}

... Or pass an instance of CompareByValue as parameter to the sort() method: ...或者将CompareByValue的实例作为参数传递给sort()方法:

Collections.sort(list, new CompareByValue());

only from above code, it looks fine. 只有从上面的代码,它看起来很好。 are you sure the exception from above code? 你确定上面代码中的例外吗? if you just put Stock object into any sorted collection, you will see such exception and need to implement Comparable interface. 如果您只将Stock对象放入任何已排序的集合中,您将看到此类异常并需要实现Comparable接口。

But for the case where just pass your custom Comparator, you don't need to make Stock to be a Comparable. 但是对于只通过自定义Comparator的情况,您不需要使Stock成为Comparable。 it is same case like you give anonymous Comparator implementation. 就像你给匿名的Comparator实现一样。

set your comparator to sorted collection like TreeSet and then add the stock object into sorted collection, it should work without implemting Comparable interface. 将比较器设置为像TreeSet这样的已排序集合,然后将stock对象添加到sorted集合中,它应该可以在不实现Comparable接口的情况下工作。

public class Student implements Comparator<Student> {

                private String name;
                private String surname;
                private int matpaz;
                private int progrpaz;
                private int anglupaz;
                private double average;

            @Override
                public int compare(Student o1, Student o2) {

                    String v1 = o1.surname;
                    String v2 = o2.surname;

                    if ((v1.compareTo(v2)) >0) return 1;
                    else return 0;
                }


                public static void alphabetically (Student[] s)
                {
                    Arrays.sort(s); 
                    System.out.printf(Arrays.toString(s));
                } 
}

暂无
暂无

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

相关问题 列表到TreeSet的转换产生:“ java.lang.ClassCastException:MyClass无法转换为java.lang.Comparable” - List to TreeSet conversion produces: “java.lang.ClassCastException: MyClass cannot be cast to java.lang.Comparable” java.lang.ClassCastException: 不能转换为 java.lang.Comparable - java.lang.ClassCastException: cannot be cast to java.lang.Comparable java.lang.ClassCastException:java.util.HashMap无法强制转换为java.lang.Comparable - java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.Comparable java.lang.ClassCastException:class java.util.TreeMap 无法转换为 class java.lang.Comparable - java.lang.ClassCastException: class java.util.TreeMap cannot be cast to class java.lang.Comparable &#39;java.lang.ClassCastException:资源无法强制转换为java.lang.Comparable&#39; - 'java.lang.ClassCastException: Resource cannot be cast to java.lang.Comparable' 线程“主”java.lang.ClassCastException 中的异常:setcollection.Enseignant 无法转换为 java.lang.Comparable - Exception in thread “main” java.lang.ClassCastException: setcollection.Enseignant cannot be cast to java.lang.Comparable java.lang.ClassCastException:com.MyComp.model.Image无法转换为java.lang.Comparable - java.lang.ClassCastException: com.MyComp.model.Image cannot be cast to java.lang.Comparable java.lang.ClassCastException:jdk.nashorn.internal.objects.NativeArray无法转换为java.lang.Comparable - java.lang.ClassCastException: jdk.nashorn.internal.objects.NativeArray cannot be cast to java.lang.Comparable 线程“主”java.lang.ClassCastException 中的异常:proj.Car 无法转换为 java.lang.Comparable - Exception in thread "main" java.lang.ClassCastException: proj.Car cannot be cast to java.lang.Comparable java.lang.ClassCastException:尝试排序List时,java.util.LinkedHashMap无法转换为java.lang.Comparable异常 - java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to java.lang.Comparable exception when trying sort a List
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM