public class HashCodeBuilder extends Object implements Builder<Integer>
Object.hashCode()方法
下面是采取的方法。添加数据字段时,将当前总数乘以乘数,然后添加该数据类型的相关值。 例如,如果当前的hashCode是17,而乘数是37,整数45将创建一个674的散列代码,17 * 37 + 45.
public class Person {
String name;
int age;
boolean smoker;
...
public int hashCode() {
return new HashCodeBuilder(17, 37).
append(name).
append(age).
append(smoker).
toHashCode();
}
}
| Constructor and Description |
|---|
HashCodeBuilder() |
HashCodeBuilder(int initialOddNumber,
int multiplierOddNumber)
必须输入两个随机选择的奇数。理想情况下,
每个类应该是不同的,但是这不是很重要.
|
| Modifier and Type | Method and Description |
|---|---|
HashCodeBuilder |
append(boolean value) |
HashCodeBuilder |
append(boolean[] array) |
HashCodeBuilder |
append(byte value) |
HashCodeBuilder |
append(byte[] array) |
HashCodeBuilder |
append(char value) |
HashCodeBuilder |
append(char[] array) |
HashCodeBuilder |
append(double value) |
HashCodeBuilder |
append(double[] array) |
HashCodeBuilder |
append(float value) |
HashCodeBuilder |
append(float[] array) |
HashCodeBuilder |
append(int value) |
HashCodeBuilder |
append(int[] array) |
HashCodeBuilder |
append(long value) |
HashCodeBuilder |
append(long[] array) |
HashCodeBuilder |
append(Object object) |
HashCodeBuilder |
append(Object[] array) |
HashCodeBuilder |
append(short value) |
HashCodeBuilder |
append(short[] array) |
HashCodeBuilder |
appendSuper(int superHashCode) |
Integer |
build()
构建
|
int |
hashCode() |
static int |
reflectionHashCode(int initialNonZeroOddNumber,
int multiplierNonZeroOddNumber,
Object object) |
static int |
reflectionHashCode(int initialNonZeroOddNumber,
int multiplierNonZeroOddNumber,
Object object,
boolean testTransients) |
static <T> int |
reflectionHashCode(int initialNonZeroOddNumber,
int multiplierNonZeroOddNumber,
T object,
boolean testTransients,
Class<? super T> reflectUpToClass,
String... excludeFields)
使用反射从
Object的字段构建有效的散列代码 |
static int |
reflectionHashCode(Object object,
boolean testTransients) |
static int |
reflectionHashCode(Object object,
Collection<String> excludeFields) |
static int |
reflectionHashCode(Object object,
String... excludeFields) |
int |
toHashCode() |
public HashCodeBuilder()
public HashCodeBuilder(int initialOddNumber,
int multiplierOddNumber)
initialOddNumber - 用作初值的奇数multiplierOddNumber - 用作乘法器的奇数public static int reflectionHashCode(int initialNonZeroOddNumber,
int multiplierNonZeroOddNumber,
Object object)
public static int reflectionHashCode(int initialNonZeroOddNumber,
int multiplierNonZeroOddNumber,
Object object,
boolean testTransients)
public static <T> int reflectionHashCode(int initialNonZeroOddNumber,
int multiplierNonZeroOddNumber,
T object,
boolean testTransients,
Class<? super T> reflectUpToClass,
String... excludeFields)
Object的字段构建有效的散列代码
它使用AccessibleObject.setAccessible 获得对私有字段的访问权。
这意味着如果权限没有正确设置,则在安全管理器下运行时将抛出安全异常。它也没有显式测试那么有效.
T - 所涉及对象的类型initialNonZeroOddNumber - initialNonZeroOddNumber非零,奇数作为初始值。如果在散列代码中没有找到包含的字段,则返回该值multiplierNonZeroOddNumber - 用作乘法器的非零的奇数object - 用于创建hashCode的对象testTransients - 是否包含瞬态字段reflectUpToClass - 反映到(包括)的超类可以是nullexcludeFields - 在计算哈希码时要排除的字段名数组public static int reflectionHashCode(Object object, boolean testTransients)
public static int reflectionHashCode(Object object, Collection<String> excludeFields)
public HashCodeBuilder append(boolean value)
public HashCodeBuilder append(boolean[] array)
public HashCodeBuilder append(byte value)
public HashCodeBuilder append(byte[] array)
public HashCodeBuilder append(char value)
public HashCodeBuilder append(char[] array)
public HashCodeBuilder append(double value)
public HashCodeBuilder append(double[] array)
public HashCodeBuilder append(float value)
public HashCodeBuilder append(float[] array)
public HashCodeBuilder append(int value)
public HashCodeBuilder append(int[] array)
public HashCodeBuilder append(long value)
public HashCodeBuilder append(long[] array)
public HashCodeBuilder append(Object object)
public HashCodeBuilder append(Object[] array)
public HashCodeBuilder append(short value)
public HashCodeBuilder append(short[] array)
public HashCodeBuilder appendSuper(int superHashCode)
public int toHashCode()
Copyright © 2019. All rights reserved.