-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHashMaps.java
68 lines (53 loc) Β· 1.71 KB
/
HashMaps.java
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package day10;
import java.util.Map;
import java.util.HashMap;
public class HashMaps {
/*
list f: index (integer) --> value (anything)
int --> anything
map: anything --> anything
key (unique) --> value (duplicate)
anything --> anything
*/
/*
ArrayList<Buckets>
Bucket --> red black tree
red black tree --> list
list<list<Entry>>
Entry pair<Key, Value>
key.hashCode() % array.length --> index
search bucket O(60)
retrieve: O(1)
*/
public static void main(String[] args) {
Map<String, Integer> words = new java.util.HashMap<>();
/*
2 objects that are equal should have same hashCode
@Override equals must @Override hashCode
-- try
2 objects that are bot equal should have different hashcode
*/
words.put("i", 100);
words.put("am", 100);
words.put("batman", 100);
// System.out.println(words.get("batman"));
// words.remove("i");
// System.out.println(words);
// for (Map.Entry<String, Integer> entry : words.entrySet()) {
// System.out.println(entry.getKey());
// System.out.println(entry.getValue());
// }
// System.out.println(words.containsKey("i"));
// System.out.println(words.get("world"));
// System.out.println(words.size());
// for (int frequency: words.values()) {
// System.out.println(frequency);
// }
words.replace("i", 2);
words.replace("hello", 0);
System.out.println(words);
if (words.containsKey("hello")) {
words.put("hello", 0);
}
}
}