Comparing different type of Objects with comparable

*爱你&永不变心* 提交于 2019-12-02 07:40:58

You could add a common base class and implement comparison there, as in:

abstract class AandBComparable implements Comparable {

  public int compareTo(Object o) {
    AandBComparable ab = (AandBComparable) o;
    return getId().compareTo(ab.getId());
  }

  public abstract String getId();
}

To be able to sort a list, its elements must be comparable to each other. That's not the case here. Instances of A can only be compared with other instances of A. Same for B.

If you want to sort a list containg A and B instances, you need to provide Comparator which will happily take two As, two Bs or an A and a B, and compare these objects as you want them compared.

public class AOrBComparator implements Comparator<Object> {
    @Override
    public int compare(Object o1, Object o2) {
        String o1Id = getId(o1);
        String o2Id = getId(o2);
        return o1Id.compareTo(o2Id);
    }

    private String getId(Object o) {
        if (o instanceof A) {
            return ((A) o).getId();
        }
        else if (o instanceof B) {
            return ((B) o).getId();
        }
        else {
            throw new IllegalArgumentException("Can only get ID from A or B");
        }
    }
}

But maybe A and B should implement the same Identifiable interface, and the list should be a List<Identifiable>. This way, you could easily write a comparator that compares two instances of Identifiable, and it would work whether the actual instance is A, B, or any other Identifiable.

I do not believe the exception raises where you tell, but rather in a call to sort()

Anyway, it means something like

"The sort method expects a List of elements that implement comparable. But you tell him that you are giving a list of Object, and not all Object implement Comparable. So, the compiler cannot be sure that at realtime the objects passed in the list will implement Comparable (as required), and throws an error"

The solution? Define the list with a bound class that implements Comparable

 List<A> listAll = new ArrayList<A>(); 

Update: To have all items in the same list then either:

a) Have all items derive from a common class / interface that implements Comparable. Usually this would be the most Object Oriented Programming friendly approach, since if you want to compare both classes they must be somehow related. Either extend B from A, A from B, A and B from another class, or make A and B implement another interface (which itself implements Comparable).

b) As JBNizet said, use a Comparator.

Again, I strongly recommend using the a solution.

You need a base (dummy) class which will implement Comparable and let A and B derive from class. Something like:

public abstract class MyComparableClass implements Comparable {
}

public class A extends MyComparableClass {
... // implementation of compareTo
}

public class B extends MyComparableClass {
...// implementation of compareTo
}

In your main program then simply define the Collection to be:

List<MyComparableClass> listAll = new ArrayList<MyComparableClass>();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!