[英]What is the efficient and fastest way to check a list of regex pattern on a hashmap in java
[英]What is the fastest and efficient way to check Deep Equal for two java objects?
我有两个java对象,其byte[]
字段的大小为数百万 。 检查这些java对象的Deep Equal的最快捷有效的方法是什么?
样本实体:
@Entity
public class NormalBook
{
@Id
private String bookId;
@Column
private String title;
@Column
private byte[] pdfFile;
//setters and getters
}
注意:我正在为ORM工具执行此操作,基本上我正在使用Persistence Context中存在的对象检查对象( 处于托管状态 )。
覆盖equals()
或者有一个* helper方法(错误选项!)并分五步完成:
1. Check for *not null*.
2. Check for same *type*.
3. Check for *size of byte[]*.
4. Check for `==` (*reference equality* of byte[])
5. Start comparing byte values
在对象类的equals()
定义中使用以下内容:
java.util.Arrays.equals(bs1, bs2)
您可能还想先检查它们是否是相同的数组(实例)。 虽然这种方法无论如何都可能做到这一点。
例如(并对包含数组的类做出一些假设):
public boolean equals(Object obj) {
if(this == obj)
return true;
if(!(obj instanceof MyObject)) // covers case where obj null, too.
return false;
return Arrays.equals(this.bytes, ((MyObject)obj).bytes);
}
如果您的班级中还有其他字段,则您的equals()
应考虑这些字段。
(如果您可以提供有关阵列中存储的数据类型的更多信息,则可能有更好的答案。)
如果你的类有像byte[]
这样的字段你可以使用类似的东西:
public class MyClass {
byte[] a;
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MyClass other = (MyClass) obj;
if (!Arrays.equals(a, other.a))
return false;
return true;
}
}
如果您关注性能并且可以确保唯一的hascode
(这是重要的hascode
必须是unique
),那么您可以只比较hascode。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.