Understanding the generated hash code override in Java, line by line

Photo by Pepi Stojanovski on Unsplash

Generated hash code function for the money class

    @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + cents;
result = prime * result + ((currencyID == null) ? 0 :
currencyID.hashCode());
result = prime * result + (int) (units ^ (units >>> 32));
return result;
}
    result = prime * result + if (currencyID == null) 0 else
currencyID.hashCode
    result = prime * result + currencyID.hashCode()
        result = prime * result + currencyID.getNumericCode();
    @Test
void testEqualsHashCodeContract() {
Currency dollars = Currency.getInstance("USD");
CurrencyAmount hundredBucks = new CurrencyAmount(100L,
dollars);
Currency alsoDollars = Currency.getInstance(Locale.US);
CurrencyAmount hundredBenjamins = new CurrencyAmount(100L,
alsoDollars);
assertEquals(hundredBucks, hundredBenjamins);
assertEquals(hundredBucks.hashCode(),
hundredBenjamins.hashCode());
}
CAD26443.30 hashed as 1414437433
$26443.30 hashed as -238002994
EUR26443.30 hashed as -1803270904
CHF26443.30 hashed as 1267727508
CAD18614.95 hashed as 1414492069
$18614.95 hashed as -237948358
EUR18614.95 hashed as -1803216268
CHF18614.95 hashed as 1267782144
CAD21553.77 hashed as 1414477710
$21553.77 hashed as -237962717
EUR21553.77 hashed as -1803230627
CHF21553.77 hashed as 1267767785
CAD1282.61 hashed as 1414442063
CAD69725.26 hashed as 1414476871
CAD-23924.63 hashed as 1414466626
CAD-18643.60 hashed as 1414458462
CAD-13755.80 hashed as 1414472794
CAD161556.37 hashed as 1414579273
$-237.65 hashed as -237995566
$-10219.12 hashed as -238036517
$100779.35 hashed as -237923853
$26845.48 hashed as -237985294
$80916.94 hashed as -237887017
$-5455.66 hashed as -237989387
CHF81439.13 hashed as 1267766167
CHF113341.48 hashed as 1267831704
CHF74645.3 hashed as 1267749763
EUR66963.90 hashed as -1803172724
EUR121545.46 hashed as -1803160426
EUR62057.93 hashed as -1803174747
EUR33135.89 hashed as -1803207513
EUR41447.91 hashed as -1803197279
    @Test
void testHashCodeDistribution() {
HashSet<CurrencyAmount> amounts = randomAmounts();
HashSet<Integer> hashes = hashCodes(amounts);
int[] hashMods = new int[32];
int modHold;
for (int hash : hashes) {
modHold = hash % 32;
if (modHold < 0) modHold += 32;
hashMods[modHold]++;
}
for (int m = 0; m < 32; m += 4) {
for (int n = m; n < m + 4; n++) {
System.out.print(n + ": " + hashMods[n] + " ");
assertTrue(hashMods[n] > 16);
assertTrue(hashMods[n] < 48);
}
System.out.println();
}
}
 0: 32     1: 37     2: 25     3: 32
4: 28 5: 29 6: 37 7: 33
8: 34 9: 29 10: 23 11: 34
12: 27 13: 32 14: 39 15: 26
16: 27 17: 28 18: 27 19: 30
20: 39 21: 27 22: 30 23: 29
24: 29 25: 37 26: 31 27: 41
28: 28 29: 37 30: 32 31: 31
        result = prime * result * currencyID.getNumericCode();
 0: 27     1: 28     2: 37     3: 23
4: 31 5: 30 6: 35 7: 26
8: 32 9: 25 10: 28 11: 25
12: 45 13: 39 14: 39 15: 28
16: 29 17: 22 18: 40 19: 15
    @Override
public int hashCode() {
int hash = 7;
hash = 89 * hash + (int) (this.units ^ (this.units >>> 32));
hash = 89 * hash + this.cents;
hash = 89 * hash + Objects.hashCode(this.currencyID);
return hash;
}
EUR134149.95 hashed as 1116150404
EUR118999.35 hashed as 996141914
EUR173109.87 hashed as 1424751852
EUR16706.84 hashed as 185883422
EUR122704.92 hashed as 1025494292
EUR532.57 hashed as 57766765
$94576.93 hashed as -1473947607
$131636.96 hashed as -1180395080
$120253.75 hashed as -1270561692
$116057.64 hashed as -1303799187
$53691.36 hashed as -1797802765
CAD143940.94 hashed as -1342037994
CAD37324.44 hashed as 2108419516
CAD531.26 hashed as 1816980561
CAD119154.19 hashed as -1538374575
CAD73224.80 hashed as -1902180676
CAD157369.89 hashed as -1235667330
CHF149643.41 hashed as 1515594358
CHF12928.69 hashed as 432677335
CHF6791.82 hashed as 384067315
 0: 23     1: 27     2: 41     3: 18    
4: 28 5: 28 6: 37 7: 27
8: 33 9: 40 10: 26 11: 27
12: 34 13: 36 14: 31 15: 30
16: 36 17: 28 18: 39 19: 39
20: 29 21: 21 22: 30 23: 48
 0: 24     1: 44     2: 28     3: 37    
4: 24 5: 33 6: 33 7: 39
8: 28 9: 36 10: 23 11: 33
12: 27 13: 31 14: 25 15: 27
16: 23 17: 36 18: 32 19: 39
20: 21 21: 36 22: 41 23: 41
24: 31 25: 36 26: 23 27: 26
28: 32 29: 34 30: 25 31: 32
    @Override
public int hashCode() {
return Objects.hash(units, cents, currencyID);
}

Generated hash code function for the transaction class

    @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((amount == null) ? 0 :
amount.hashCode());
result = prime * result + ((dateTime == null) ? 0 :
dateTime.hashCode());
return result;
}
@Override
public String toString() {
return this.getClass().getName() + " for " +
this.amount.toString() + " on " + this.dateTime.toString();
}
    private static final Currency DOLLARS =
Currency.getInstance("USD");
private static final LocalDateTime NEW_YEARS_EVE =
LocalDateTime.of(2019, Month.DECEMBER, 31, 11, 59);

private static ArrayList<Transaction> transactions = new
ArrayList<>();
private static void addPayChecks() {
CurrencyAmount payCheckAmount = new CurrencyAmount(1000,
DOLLARS);
LocalDateTime payDay = LocalDateTime.of(2019, Month.JANUARY,
4, 7, 0);
Deposit payCheck;
while (payDay.isBefore(NEW_YEARS_EVE)) {
payCheck = new Deposit(payCheckAmount, payDay);
transactions.add(payCheck);
payDay = payDay.plusDays(7);
}
}

private static void addRentPayments() {
CurrencyAmount rentAmount = new CurrencyAmount(-1000,
DOLLARS);
LocalDateTime rentDay = LocalDateTime.of(2019,
Month.JANUARY, 7, 12, 0);
Withdrawal rentPayment;
while (rentDay.isBefore(NEW_YEARS_EVE)) {
rentPayment = new Withdrawal(rentAmount, rentDay);
transactions.add(rentPayment);
rentDay = rentDay.plusDays(28);
}
}
bankaccounts.transactions.Deposit for $1000.00 on 2019-01-04T07:00
bankaccounts.transactions.Deposit for $1000.00 on 2019-01-11T07:00
bankaccounts.transactions.Deposit for $1000.00 on 2019-01-18T07:00
...
bankaccounts.transactions.Deposit for $1000.00 on 2019-12-27T07:00
bankaccounts.transactions.Withdrawal for $1000.00 on 2019-01-07T12:00
...
bankaccounts.transactions.Withdrawal for $1000.00 on 2019-12-09T12:00
bankaccounts.transactions.Withdrawal for $500.00 on 2019-01-28T11:00
...
bankaccounts.transactions.Withdrawal for $500.00 on 2019-12-05T11:00
    @Override
public int hashCode() {
int hash = 3;
hash = 17 * hash + Objects.hashCode(this.amount);
hash = 17 * hash + Objects.hashCode(this.dateTime);
return hash;
}
    @Override
public int hashCode() {
return Objects.hash(transactionAmount, transactionDate);
}
Photo by Marina Khrapova on Unsplash

In the next part

is a composer and photographer from Detroit, Michigan. He has been working on a Java program to display certain mathematical diagrams.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store