Google番石榴哈希

Google Guava Hashing

我对番石榴漏斗有一些问题,我阅读了这篇文章 https://code.google.com/p/guava-libraries/wiki/HashingExplained 和其他文章,但我不知道如何在我的 class 不仅包含基本类型时使用漏斗。

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 class 没有 putObject 方法,只有 Hasher class 有。 我可以将 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);
  }
};