-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEqualsAndHashCodeExample.java
More file actions
47 lines (36 loc) · 1.16 KB
/
EqualsAndHashCodeExample.java
File metadata and controls
47 lines (36 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
* Copyright (c) Romanov Alexey, 2024
*/
package ru.romanow.memory.leaks;
import java.util.HashMap;
import java.util.Scanner;
import static ru.romanow.memory.leaks.utils.ExampleSupport.logMemory;
public class EqualsAndHashCodeExample {
public static void main(String[] args) {
logMemory();
final var scanner = new Scanner(System.in);
final var map = new HashMap<KeyHolder, Integer>();
while (true) {
for (int i = 0; i < 25_000; i++) {
map.put(new KeyHolder("key"), i);
}
System.out.println("Map size: " + map.size());
map.remove(new KeyHolder("key"));
System.out.println("Map size after remove: " + map.size());
logMemory();
System.out.println("Continue? (Ctrl+C for exit)");
scanner.nextLine();
}
}
@SuppressWarnings("ClassCanBeRecord")
private static class KeyHolder {
private final String key;
public KeyHolder(String key) {
this.key = key;
}
public String key() {
return key;
}
}
// private record KeyHolder(String key) {}
}