Checking the queue in the object

When a java.util.concurrent.BlockingQueue

According to the Java specification for the contains(Object o) method

If I previously inserted a new object, for example:

 Task task = new Task("taskname", "somevalue"); queue.put(task); 

On him. And later try to do this:

 Task task = new Task("taskname", "somevalue"); queue.contains(task); 

Since BlockingQueue is just an interface, according to the Java specification, should it return true or not?

The Task class is Serializable , so the comparison will be based on the field values ​​on the right?

+4
source share
3 answers

The behavior depends on whether the Task class overrides the equals method. Depending on the logic of the equals method, these two tasks may/may not are equal.

From java queue locking documents

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 Object method to check for equality (which checks if references to objects are equal).

  public boolean equals(Object obj) { return (this == obj); } 

Since these are two different objects, therefore the identifier of the object link will be different, and therefore contains will return false .

+9
source

Since BlockingQueue is just an interface, according to the Java specification, if this is true or not?

This is a strange question. While the queue is being created, it should behave as promised by its interface ( BlockingQueue ).

The interface is abstract in that it cannot be created by itself, but it is a general contract for all objects created by those classes that implement it.

As for your layman question, does queue.contains(task) return true depend on how the Task class defines its equals method.

+1
source

It depends on what logic you write in the contains() method. When you try to create an instance of BlockingQueue<Object> , you must implement several methods, and contains() from them.

 BlockingQueue<Object> a = new BlockingQueue<Object>() { @Override public Object remove() { // TODO Auto-generated method stub return null; } @Override public Object poll() { // TODO Auto-generated method stub return null; } @Override public Object element() { // TODO Auto-generated method stub return null; } @Override public Object peek() { // TODO Auto-generated method stub return null; } @Override public int size() { // TODO Auto-generated method stub return 0; } @Override public boolean isEmpty() { // TODO Auto-generated method stub return false; } @Override public Iterator<Object> iterator() { // TODO Auto-generated method stub return null; } @Override public Object[] toArray() { // TODO Auto-generated method stub return null; } @Override public <T> T[] toArray( T[] a ) { // TODO Auto-generated method stub return null; } @Override public boolean containsAll( Collection< ? > c ) { // TODO Auto-generated method stub return false; } @Override public boolean addAll( Collection< ? extends Object> c ) { // TODO Auto-generated method stub return false; } @Override public boolean removeAll( Collection< ? > c ) { // TODO Auto-generated method stub return false; } @Override public boolean retainAll( Collection< ? > c ) { // TODO Auto-generated method stub return false; } @Override public void clear() { // TODO Auto-generated method stub } @Override public boolean add( Object e ) { // TODO Auto-generated method stub return false; } @Override public boolean offer( Object e ) { // TODO Auto-generated method stub return false; } @Override public void put( Object e ) throws InterruptedException { // TODO Auto-generated method stub } @Override public boolean offer( Object e, long timeout, TimeUnit unit ) throws InterruptedException { // TODO Auto-generated method stub return false; } @Override public Object take() throws InterruptedException { // TODO Auto-generated method stub return null; } @Override public Object poll( long timeout, TimeUnit unit ) throws InterruptedException { // TODO Auto-generated method stub return null; } @Override public int remainingCapacity() { // TODO Auto-generated method stub return 0; } @Override public boolean remove( Object o ) { // TODO Auto-generated method stub return false; } @Override public boolean contains( Object o ) { // TODO Auto-generated method stub return false; } @Override public int drainTo( Collection< ? super Object> c ) { // TODO Auto-generated method stub return 0; } @Override public int drainTo( Collection< ? super Object> c, int maxElements ) { // TODO Auto-generated method stub return 0; } }; 
0
source

All Articles