谷歌番石榴哈希

时间:2015-05-07 11:11:11

标签: hash 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方法,但可能有人知道更好的方法。

1 个答案:

答案 0 :(得分:3)

你是对的:目前,只能按照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);
  }
};
相关问题