由于您的doesNotContain(..) 实现接近于containsAll(..) 实现,我建议使用那个。如果您执行list1.containsAll(list2),实现将遍历list2 的所有元素以检查list1 中是否存在相等的对象。这就是您需要在 LocalDate 中覆盖 public boolean equals(Object obj) 的原因。
在下面找到一个小例子来展示 containsAll(..) 所做的事情
运行检查的主要过程
public static void main(String[] args) throws Exception {
List<LocalDate> list1 = new LinkedList<>();
list1.add(new LocalDate("112233"));
list1.add(new LocalDate("223344"));
List<LocalDate> list2 = new LinkedList<>();
list2.add(new LocalDate("112233"));
list2.add(new LocalDate("112233"));
System.out.println("list1 = " + list1);
System.out.println("list2 = " + list2);
System.out.println("list1.containsAll(list2) = " + list1.containsAll(list2));
System.out.println("list2.containsAll(list1) = " + list2.containsAll(list1));
}
如果您在不覆盖 equals 方法的情况下实现 LocalDate,则只有在比较 same 对象的引用时,equals 才会为真。
// a naive implementation for demonstration purpose
class LocalDate {
String hour;
String minute;
String second;
LocalDate(String string) {
hour = string.substring(0, 2);
minute = string.substring(2, 4);
second = string.substring(4, 6);
}
@Override
public String toString() {
return String.format("LocalDate{%s%s%s} - hashcode: %d", hour, minute, second, this.hashCode());
}
}
结果是
list1 = [LocalDate{112233} - hashcode: 33039820, LocalDate{223344} - hashcode: 31311199]
list2 = [LocalDate{112233} - hashcode: 13177912, LocalDate{112233} - hashcode: 21924553]
list1.containsAll(list2) = false
list2.containsAll(list1) = false
如果你重写了 equals 方法(为了演示目的,已经省略了哈希码)
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final LocalDate other = (LocalDate) obj;
System.out.println(toString() + ".equals(" + obj.toString() + ')');
if (!Objects.equals(this.hour, other.hour)) {
return false;
}
if (!Objects.equals(this.minute, other.minute)) {
return false;
}
if (!Objects.equals(this.second, other.second)) {
return false;
}
return true;
}
输出将是
list1 = [LocalDate{112233} - hashcode: 33336787, LocalDate{223344} - hashcode: 12767201]
list2 = [LocalDate{112233} - hashcode: 31311199, LocalDate{112233} - hashcode: 13177912]
// generated output by the method containsAll(..)
LocalDate{112233} - hashcode: 31311199.equals(LocalDate{112233} - hashcode: 33336787)
LocalDate{112233} - hashcode: 13177912.equals(LocalDate{112233} - hashcode: 33336787)
list1.containsAll(list2) = true
// generated output by the method containsAll(..)
LocalDate{112233} - hashcode: 33336787.equals(LocalDate{112233} - hashcode: 31311199)
LocalDate{223344} - hashcode: 12767201.equals(LocalDate{112233} - hashcode: 31311199)
LocalDate{223344} - hashcode: 12767201.equals(LocalDate{112233} - hashcode: 13177912)
list2.containsAll(list1) = false
根据打印出来的Object.hashcode(),您可以很容易地看到 containsAll() 方法遍历了您调用该方法的列表中的所有元素。
如果您想改进检查,您需要先明确以下几点
- 列表中的元素是否唯一 -> 然后最好使用 Set
- 列表必须具有相同数量的元素 -> 如果是,您可以在第一步比较它们的大小
- 如果您只需要检查 list2 是否只有 list1 中的元素(意味着列表包含唯一值)-> 调用 list2 上的 containsAll 方法
- 之前对列表进行排序也可能是值得的(取决于包含的数据)
如果没有这些信息,就不可能在所有情况下给出the best 的建议。