java 检查队列是否包含对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14930651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking if queue contains object
提问by quarks
For a java.util.concurrent.BlockingQueue
为一个 java.util.concurrent.BlockingQueue
By Java specification, for a method contains(Object o)
根据 Java 规范,对于一个方法 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?
由于 BlockingQueue 只是一个接口,根据 Java 规范,这是否应该返回 true ?
The Task
class is Serializable
so the comparison will be based on field values right?
该Task
班是Serializable
这样的比较将根据正确的字段值?
回答by MoveFast
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.
行为取决于Task
类是否覆盖equals
方法。根据equals
方法的逻辑,这两个任务may/may not
是相等的。
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).
布尔值包含(对象 o)
如果此队列包含指定的元素,则返回 true。更正式地说,当且仅当此队列包含至少一个元素 e 使得 o.equals(e) 时才返回 true。
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).
如果该equals
方法未被覆盖,则 Java 将使用for 相等检查equals
方法Object
(检查对象引用是否相等)。
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
.
由于这是两个不同的对象,因此对象引用 id 将不同,因此contains
将返回false
。
回答by Hui Zheng
Since BlockingQueue is just an interface, by Java specification, should this return true or not?
由于 BlockingQueue 只是一个接口,根据 Java 规范,这是否应该返回 true ?
This is a weird question. As long as queue
object is created, it should behave as promised by its interface(BlockingQueue
).
这是一个奇怪的问题。只要queue
对象被创建,它的行为就应该像其 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.
至于您的 concreate 问题,是否queue.contains(task)
返回true
取决于类如何Task
定义其equals
方法。
回答by LPD
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.
这取决于您在contains()
方法中编写的逻辑是什么。当您尝试实例化时,BlockingQueue<Object>
它会迫使您实现一些方法,并且contains()
它们并不常见。
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;
}
};