Skip to content

Commit

Permalink
Containers in Depth: Exercise 37
Browse files Browse the repository at this point in the history
  • Loading branch information
kean0212 committed Jun 6, 2017
1 parent 05f1cd4 commit c2465a4
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions ContainersInDepth/HashMapTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class HashMapTest {

private void test(int initialCapacity, float loadFactor, int size) {
HashMap<String, String> hashMap = new HashMap(initialCapacity, loadFactor);
fillHashMap(hashMap, size);
long duration = countSearchingTime(hashMap);
String result = String.format("Searching in HashMap [size = %d, capacity = %d, loadFactor = %f] in %d nanoseconds on average",
size, initialCapacity, loadFactor, duration);
Util.println(result);
}

private void fillHashMap(HashMap<String, String> hashMap, int size) {
hashMap.putAll(Countries.capitals(size));
}

private long countSearchingTime(HashMap<String, String> hashMap) {
Set<Map.Entry<String, String>> entrySet = hashMap.entrySet();
long startTime = getCurrentTime();
for (Map.Entry entry : entrySet) {
hashMap.get(entry.getKey());
}
long duration = getCurrentTime() - startTime;
return duration / entrySet.size();
}

private long getCurrentTime() {
return System.nanoTime();
}

public static void main(String[] args) {
HashMapTest hashMapTest = new HashMapTest();
int capitalsSize = Countries.capitals().size();
hashMapTest.test(capitalsSize, 1.75f, capitalsSize);
hashMapTest.test(2 * capitalsSize, 1.75f, capitalsSize);
hashMapTest.test(capitalsSize, 0.25f, capitalsSize);
}
}

0 comments on commit c2465a4

Please sign in to comment.