How to cast in the compareTo method to a Generic Class which Implements Comparable?

java comparable
comparable array java
generic compareto java
java generic comparable
comparable example in java 8
implement compareto java
java comparator example sort
x435 generics - comparable cage

I have an Arraylist with type Person. Name is a class that I have created. In my mainapp I'm creating some Name objects, setting each name-object to a person object and then adding the person objects to an ArrayList. When I try to sort the list using Collections.sort(), I get a ClassCastException.

I can not see what the problem is.

Here is my code so far:

public class Person<T> implements Comparable<Person<T>> {

private T name;

public T getName() {
    return name;

public void setName(T name) { = name;

public int compareTo(Person<T> o) {
    return ((Person<T>) this.getName()).compareTo((Person<T>) o.getName());


The return value from getName() has type T, but you are trying to cast it to Person<T>. To fix the problem, you need to compare the names directly without casting:

public int compareTo(Person<T> o) {
    return this.getName().compareTo(o.getName());

Now this will cause another error because the compiler doesn't know if T has the compareTo() method. To fix that, you need to add a bound to the type T:

public class Person<T implements Comparable<T>> implements Comparable<Person<T>>


To me, this seems like overuse of generics. Why should name be allowed a generic type when String is the only natural fit? And why do you need a Name class? It is likely only a wrapper around a String so you should just use String directly.

Comparing Objects - OmarElGabry's Blog, method should throws an exception if the passed object has incompatible type or null. 1.1. compareTo () method. For any class to support natural ordering, it should implement the Comparable interface and override it’s compareTo () method. It must return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

You are casting this.getName(), which is of type T, to type Person<T>. I.e. you are casting a Name to a Person, hence the ClassCastException ("name is not a person").

To fix this, please remove the casts and compare the names directly. Ignoring null-safety, it would be:

return getName().compareTo(o.getName());

Note that class Name also needs to implements Comparable<Name>.

Java Comparable interface example, When a class implements Comparable interface which of the below method should be overridden? Comparer<T>.Compare(T, T) Method (System.Collections.Generic), This chapter describes the interfaces Comparable and Comparator , which … we will introduce bounds on type variables, an important feature of generics that In this next example, we use an anonymous inner class to compare the value of objects.

Why is your name a type? This looks a bit weird. Could it be you meant to do something like this:

public class Person implements Comparable<Person> {

    private String name;

    public Person(String name) { = name;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public int compareTo(Person other) {

Java Comparator Example for Custom Sorting , How do you sort an employee object using comparator and comparable? You can cast an Object[] into an Object[], as T will be erased into Object (or, if T extends Comparable, then into Comparable). Your code works; inserting the following main method into your code prints the expected result:

Sorting with Comparable and Comparator in Java, Using the Comparable interface and compareTo() method, we can sort using alphabetical interface in the Simpson class, using Simpson in the generic type: Now the class Object does not contain the method compareTo(). So we first create a reference of the type Comparable<T> and then use the compareTo() method. Also note that this method would work on the objects of only those classes that implement the interface Comparable and provide an appropriate definition of the method compareTo()'

Cast to Comparable<T> ???, This is a generic method to be used with an ordered array-based list to OK, so, if this class doesn't implement the compareTo() method, and  It's better to use the generic form of the interface: public class Item implements Comparable<Item> { Then you can specify Item instead of Object to compare against: @Override public int compareTo(Item i){ and you don't have to cast the Object to an Item. When calling compareTo, the compiler will enforce that the object to compare to must be an Item.

12. Generics, In Java, generic means parameterized by one or more type variable. is have the array be Object type and cast the return values to type T whenever it is required, implements Comparable<MyClass> { public int compareTo(​MyClass elt) { . You just have to define that Animal implements Comparable<Animal> i.e. public class Animal implements Comparable<Animal>. And then you have to implement the compareTo(Animal other) method that way you like it. @Override public int compareTo(Animal other) { return, other.year_discovered); }