Reputation: 35282
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?
Upvotes: 5
Views: 19814
Reputation: 3025
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
.
Upvotes: 11
Reputation: 10224
Since BlockingQueue is just an interface, by Java specification, should this return true or not?
This is a weird question. As long as queue
object is created, it should behave as promised by its interface(BlockingQueue
).
Interface is abstract in that it cannot be instantiated by itself, but it's a common contract for all objects created by those classes that implement it.
As for your concreate question, whether queue.contains(task)
return true
depends on how class Task
defines its equals
method.
Upvotes: 1
Reputation: 2883
Thats depends on what is the logic that you write in the contains()
method. When you try to instantiate BlockingQueue<Object>
is forces you to implement few methods and contains()
is ojne of 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;
}
};
Upvotes: 0