由于ArrayList类已经包装了一个数组,因此您可以扩展它并覆盖
equalsand
hashCode方法。这是一个示例:
public MyArrayList extends ArrayList<MyClass> { @Override public boolean equals(Object o) { if (o instanceof MyArrayList) { //place your comparison logic here return true; } return false; } @Override public int hashCode() { //just a sample, you can place your own pre return super.hashCode(); }}更新:
您甚至可以重写它以用于一般用途,只需将代码更改为:
public MyArrayList<T> extends ArrayList<T> { //overrides the methods you need @Override public boolean equals(Object o) { if (o instanceof MyArrayList) { //place your comparison logic here return true; } return false; }}


