I am having a weird issue which I am not being able to understand why this happens.
I have this equals
method implemented in a DoublyLinkedList
generic class:
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (getClass() != obj.getClass() || obj == null) {
return false;
}
DoublyLinkedList<E> other = (DoublyLinkedList<E>) obj;
if (this.size != other.size) {
return false;
}
Iterator<E> iterator = this.iterator();
Iterator<E> otherIterator = other.iterator();
while(iterator.hasNext()){
if(iterator.next() != otherIterator.next()){
return false;
}
}
return true;
}
Testing this method, in a unit test, like this:
@Test
public void testEquals() {
System.out.println("equals");
DoublyLinkedList <String> instance1 = new DoublyLinkedList <>(), instance2 = new DoublyLinkedList <>();
instance1.addLast("Xpto");
instance1.addLast("Ypto");
instance1.addLast("Zpto");
instance2.addLast("Xpto");
assertFalse("Lists should not be equal", (instance1.equals(instance2)));
assertFalse("Lists should not be equal", (instance2.equals(instance1)));
instance2.addLast("Ypto");
assertFalse("Lists should not be equal", (instance1.equals(instance2)));
assertFalse("Lists should not be equal", (instance2.equals(instance1)));
instance2.addLast("Zpto");
assertTrue("Lists should be equal", (instance1.equals(instance2)));
assertTrue("Lists should be equal", (instance2.equals(instance1)));
}
Gives me that the test passes. However, why does this happen, if I am using !=
, instead of equals
to compare each iterator's instance, in the first code? Shouldn't it compare the references, and thus fail?
Thanks in advance!