Ashhar Hasan - 4 months ago 5x

Java Question

`private long configKeyMasterId;`

private String configKey;

private boolean isDisplay;

public int hashCode() {

int result = (int) (configKeyMasterId ^ (configKeyMasterId >>> 32));

result = 31 * result + (configKey != null ? configKey.hashCode() : 0);

result = 31 * result + (isDisplay != null ? isDisplay.hashCode() : 0);

return result;

}

This is what I have where

`isDisplay`

Is something as naive as,

`result = 31 * result + (isDisplay != false ? Boolean.hashCode(isDisplay) : 0);`

okay?

I don't know how or why exactly the

`hashCode()`

Answer

Checking for `false`

is redundant. Just do

```
result = 31 * result + Boolean.hashCode(isDisplay);
```

**EDIT** Note that this assumes `isDisplay`

is a primitive field. If it's a boxed field (`Boolean`

), the version you posted (with the null check) should be correct.

Source (Stackoverflow)

Comments