我对 Guava 漏斗有一些问题,我读了这篇文章https://code.google.com/p/guava-libraries/wiki/HashingExplained和其他人,但我不知道当我的类不仅包含原始类型时如何使用漏斗。
Funnel<Person> personFunnel = new Funnel<Person>() {
@Override
public void funnel(Person person, PrimitiveSink into) {
into
.putInt(person.id)
.putString(person.firstName, Charsets.UTF_8)
.putString(person.lastName, Charsets.UTF_8)
.putInt(birthYear)
//.putObject(myObject,myObjectFunnel);I want to do something like this
}
};
在我需要这样做之后
HashFunction hf = Hashing.md5();
HashCode hc = hf.newHasher()
.putObject(person, personFunnel)
.hash();
PrimitiveSink 类没有 putObject 方法,只有 Hasher 类有。 我可以将 myObject 转换为字节数组并使用 putBytes 方法,但可能有人知道更好的方法。
最佳答案
你是对的:目前,仅通过 API 链式方法是不可能做到这一点的。
但我看到您有一个 myObjectFunnel
。那么为什么不使用它呢?
关于:
Funnel<Person> personFunnel = new Funnel<Person>() {
@Override
public void funnel(Person person, PrimitiveSink into) {
into
.putInt(person.id)
.putString(person.firstName, Charsets.UTF_8)
.putString(person.lastName, Charsets.UTF_8)
.putInt(birthYear);
myObjectFunnel.funnel(myObject, into);
}
};
关于hash - 谷歌 Guava 哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30099409/