Java で特定のオブジェクトのハッシュを生成したい場合、私が知っている最も簡単な方法は、Apache Commons を使用することですHashCodeBuilder
。
public class Person {
String name;
int age;
boolean smoker;
...
public int hashCode() {
// you pick a hard-coded, randomly chosen, non-zero, odd number
// ideally different for each class
return new HashCodeBuilder(17, 37).
append(name).
append(age).
append(smoker).
toHashCode();
}
}
C++ に似たようなものはありますか?