11

I am doing a java project and I got this problem and don't know how to fix it.

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

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

Anyone know what's wrong?

Mostafa Jamareh
  • 1,344
  • 3
  • 21
  • 52
user2234225
  • 119
  • 1
  • 1
  • 4

3 Answers3

16

It's because Stock isn't implementing 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:

Collections.sort(list, new CompareByValue());
Óscar López
  • 215,818
  • 33
  • 288
  • 367
0

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.

But for the case where just pass your custom Comparator, you don't need to make Stock to be a Comparable. it is same case like you give anonymous Comparator implementation.

set your comparator to sorted collection like TreeSet and then add the stock object into sorted collection, it should work without implemting Comparable interface.

Steve Park
  • 1,817
  • 22
  • 26
  • I'm sorry everyone. Just found out there's a typo in the txt file making a null object created and added to the ArrayList. Please remove this question asap. – user2234225 Oct 31 '13 at 21:46
0
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));
                } 
}
barkman345
  • 751
  • 1
  • 5
  • 6
  • The code above should sort Students names alphabetically and then print them, but i get the same error:Exception in thread "main" java.lang.ClassCastException: Student cannot be cast to java.lang.Comparable at java.util.ComparableTimSort.countRunAndMakeAscending(Unknown Source) at java.util.ComparableTimSort.sort(Unknown Source) at java.util.Arrays.sort(Unknown Source) any ideas? – barkman345 Mar 05 '19 at 20:45
  • Arrays.sort(s, (a,b) -> a.surname.compareTo(b.surname)); – barkman345 Mar 05 '19 at 22:12