-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashCollisionChecker.java
45 lines (41 loc) · 1.57 KB
/
HashCollisionChecker.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
import java.util.*;
public class HashCollisionChecker {
public static <T> int countOfUniqueHashCodes(HashSet<T> set) {
// TODO: Implement
Set<T> newSet = new HashSet<>();
Set<Integer> hashCodeCollector = new HashSet<>();
Iterator<T> it = set.iterator();
while ( it.hasNext() ) {
newSet.add(it.next());
hashCodeCollector.add(newSet.hashCode());
newSet.clear();
}
return hashCodeCollector.size();
}
public static <K, V> int countOfUniqueHashCodes(HashMap<K, V> map) {
// TODO: Implement
Set<K> set = new HashSet<>();
Set<Integer> hashCodeCollector = new HashSet<>();
Iterator<K> it = map.keySet().iterator();
while (it.hasNext()) {
set.add(it.next());
hashCodeCollector.add(set.hashCode());
set.clear();
}
return hashCodeCollector.size();
}
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("c#c#c#c#c#c#bBc#c#c#c#bBc#");
set.add("abcd");
set.add("c#c#c#c#c#c#bBc#c#c#c#c#aa");
set.add("1234");
set.add("c#c#c#c#c#c#bBc#c#c#c#c#bB");
System.out.println(countOfUniqueHashCodes(set)); // 3
HashMap<String, Integer> map = new HashMap<>();
map.put("c#c#c#c#c#c#c#aaaaaaaabBbB", 14);
map.put("c#c#c#c#c#c#c#aaaaaaaac#c#", 12);
map.put("c#c#c#c#c#c#c#aaaaaaaac#cc", 16);
System.out.println(countOfUniqueHashCodes(map)); // 2
}
}