hashCode
Returns a hash code value for this integer.
This function follows the contract of Any.hashCode, with an additional property: if two instances of Integer are not equal, then calling this function on these objects must produce different hash codes.
Calling from Kotlin
Here's an example of calling this function from Kotlin code:
fun checkEquality(integer: Integer, other: Any?) {
check(integer == other)
check(integer.hashCode() == other.hashCode())
}
fun checkDiff(integer: Integer, other: Any?) {
check(integer != other)
check(integer.hashCode() != other.hashCode())
}
checkEquality(
integer = Integer.fromLong(0),
other = Integer.parse("-000")
)
checkEquality(
integer = Integer.fromLong(42),
other = Integer.parse("+00042")
)
checkEquality(
integer = Integer.fromLong(-42),
other = Integer.parse("-0042")
)
checkDiff(integer = Integer.fromLong(0), other = Integer.fromLong(1))
checkDiff(integer = Integer.fromLong(42), other = 42)
checkDiff(integer = Integer.fromLong(-42), other = null)Content copied to clipboard
Calling from Java
Here's an example of calling this function from Java code:
final BiConsumer<Integer, Object> checkEquality = (integer, other) -> {
final boolean equality = Objects.equals(integer, other);
final boolean hashConformity =
Objects.hashCode(integer) == Objects.hashCode(other);
final boolean check = equality && hashConformity;
if (!check) throw new IllegalStateException("Check failed.");
};
final BiConsumer<Integer, Object> checkDiff = (integer, other) -> {
final boolean equality = !Objects.equals(integer, other);
final boolean hashConformity =
Objects.hashCode(integer) != Objects.hashCode(other);
final boolean check = equality && hashConformity;
if (!check) throw new IllegalStateException("Check failed.");
};
checkEquality.accept(Integer.fromLong(0), Integer.parse("-000"));
checkEquality.accept(Integer.fromLong(42), Integer.parse("+00042"));
checkEquality.accept(Integer.fromLong(-42), Integer.parse("-0042"));
checkDiff.accept(Integer.fromLong(0), Integer.fromLong(1));
checkDiff.accept(Integer.fromLong(42), 42);
checkDiff.accept(Integer.fromLong(-42), null);Content copied to clipboard