[英]How do I check if an element exists in a queue ( LinkedList ) before adding it?
How do I check if an element exists in a queue ( LinkedList ) before adding it ?如何在添加元素之前检查队列 (LinkedList) 中是否存在元素?
EX: Address [id=1, url=https://www.google.com, size=2, queue=[request [requestId=1], request [requestId=2]]] EX:地址 [id=1, url=https://www.google.com, size=2, queue=[request [requestId=1], request [requestId=2]]]
I can't add queue=[request [requestId=1], request [requestId=1]] because they have the same requestId.我无法添加 queue=[request [requestId=1], request [requestId=1]] 因为它们具有相同的 requestId。
you can use contains(Object o)
method你可以使用
contains(Object o)
方法
contains description:包含描述:
Returns true if this list contains the specified element.
如果此列表包含指定元素,则返回 true。 More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).
更正式地说,当且仅当此列表包含至少一个元素 e 满足 (o==null ? e==null : o.equals(e)) 时,才返回 true。 [...]
[...]
Parameters: o – element whose presence in this list is to be tested
参数: o – 要测试其在此列表中的存在的元素
Returns: true if this list contains the specified element
返回:如果此列表包含指定元素,则为 true
LinkedList
implements the Collection
interface, which defines the Collection#contains
method. LinkedList
实现了Collection
接口,该接口定义了Collection#contains
方法。
You can do what you're after with any class that derives from Collection
easily by just using the contains
method.只需使用
contains
方法,您就可以轻松地使用从Collection
派生的任何类来完成您所追求的事情。
See the docs on LinkedList#contains
for more information.有关详细信息,请参阅
LinkedList#contains
上的文档。
Note: It uses Objects#equals
to determine if the item is in the Collection
.注意:它使用
Objects#equals
来确定该项目是否在Collection
中。
If you have the same reference, I think you'll be good.如果你有相同的参考,我想你会很好。 If you have different references representing the same data, then you may need to
@Override
the equals
method on your object.如果您有代表相同数据的不同引用,那么您可能需要
@Override
对象上的equals
方法。
Below code may help you...下面的代码可以帮助你...
package com.example.Solution;
import java.util.LinkedList;
import java.util.Queue;
public class Sol {
public static void main(String[] args){
Queue<Address> q1= new LinkedList<>();
q1.add(new Address("abc",1,"url1"));
Address a1=new Address("abc",1,"url");
if(!q1.contains(a1))
q1.add(a1);
Address a2=new Address("abc",2,"url");
if(!q1.contains(a2)){
q1.add(a2);
}
Address a3=new Address("abc",1,"url2");
if(!q1.contains(a3)){
q1.add(a3);
}
System.out.println(q1.size());
}
}
class Address {
String name;
int id;
String url;
public Address(String name, int id, String url) {
this.name = name;
this.id = id;
this.url = url;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public boolean equals(Object o1){
Address address=(Address) o1;
return address.getId()==this.id;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.