我想快速从Java Set中获取与现有对象相等的对象.有没有比迭代所有元素的更快的方法?
这是我的代码:
class A {
int a,b,c,d;
public A(int a, int b, int c, int d) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + getOuterType().hashCode();
result = prime * result + a;
result = prime * result + b;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
A other = (A) obj;
if (!getOuterType().equals(other.getOuterType()))
return false;
if (a != other.a)
return false;
if (b != other.b)
return false;
return true;
}
private Main getOuterType() {
return Main.this;
}
}
Run Code Online (Sandbox Code Playgroud)
并在代码中:
void run() {
Set<A> a = new HashSet<>();
a.add(new A(1,2,3,4));
a.add(new A(2,3,4,5));
A b = new A(1,2,3,5);
//How to fetch from set a object equal to object b?
}
Run Code Online (Sandbox Code Playgroud)
是否可以在Groovy中快速完成?
界面中没有get方法java.util.Set.因此,您无法获取条目:)
也许您使用的是错误的数据结构.可能你需要的是java.util.Map什么?
| 归档时间: |
|
| 查看次数: |
157 次 |
| 最近记录: |