Is there a way to tell if Two Array Deques are equal in Java? Stack has an .equals() operator?
What is data values equals method for Array Deque? this is what I wrote temporarily, however it removes items from the Deque. If there is no library method, any way to optimize code below?
ArrayDeque<Character> sQueue = new ArrayDeque<Character>();
ArrayDeque<Character> tQueue = new ArrayDeque<Character>();
if (sQueue.size() != tQueue.size()) return false;
while (!sQueue.isEmpty()) {
if (sQueue.removeLast() != tQueue.removeLast()) {
return false;
}
}
return true;
Question Context: for people who use Leetcode Backspace String Compare
Also, is the original Equals for ArrayDeque just checking thememory pointer variable? this does not seem to work.
Resource: Why doesn't ArrayDeque override equals() and hashCode()?

Your code probably would work if you don't use
!=but instead this:There's one issue with your code - it removes all contents of the queue. To prevent that, besides using
toArray()as suggested in one of the comments, you an use anIterator;Dequemandates that it returns "the elements in this deque in proper sequence":You can do it without the length check, by making two changes:
while (sIterator.hasNext()) {withwhile (sIterator.hasNext() && tIterator.hasNext()) {- keep looping until at least one of the iterators is exhausted.return true;withreturn sIterator.hasNext() == tIterator.hasNext();- returntrueonly if both iterators are exhausted.