-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathHashCollisionChecker.java
More file actions
39 lines (34 loc) · 1.29 KB
/
HashCollisionChecker.java
File metadata and controls
39 lines (34 loc) · 1.29 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
import com.sun.source.tree.CompilationUnitTree;
import java.util.*;
public class HashCollisionChecker {
public static <T> int countOfUniqueHashCodes(HashSet<T> set) {
// TODO: Implement
HashSet<Integer> uniqueHashCodes = new HashSet<>();
for (T item : set) {
uniqueHashCodes.add(item.hashCode());
}
return uniqueHashCodes.size();
}
public static <K, V> int countOfUniqueHashCodes(HashMap<K, V> map) {
// TODO: Implement
HashSet<Integer> uniqueHashCodes = new HashSet<>();
for (K key : map.keySet()) {
uniqueHashCodes.add(key.hashCode());
}
return uniqueHashCodes.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
}
}