Checking if queue contains object
java
Solution
The behavior depends on if `Task` class overrides `equals` method. Depending on the logic of `equals` method, these two Tasks `may/may not` be equal.
From the Java docs of Blocking queue
boolean contains(Object o)
Returns true if this queue contains the specified element. More formally, returns true if and only if this queue contains at least one element e such that o.equals(e).
If the `equals` method is not overridden, then Java will use the `equals` method of `Object` for equality check(which checks if object references are equal).
public boolean equals(Object obj) {
return (this == obj);
}
Since these are two distinct objects, so object reference id will be different and hence `contains` will return `false`.
Problem
For a `java.util.concurrent.BlockingQueue` By Java specification, for a method `contains(Object o)` If I have previously inserted a new object like: ``` Task task = new Task("taskname", "somevalue"); queue.put(task); ``` on it. And later try to do this: ``` Task task = new Task("taskname", "somevalue"); queue.contains(task); ``` Since BlockingQueue is just an interface, by Java specification, should this return true or not? The `Task` class is `Serializable` so the comparison will be based on field values right?