Woodstock Blog

a tech blog for general algorithmic interview questions

[CC150v4] 14.6 Java HashMap Counter


Suppose you are using a map in your program, how would you count the number of times the program calls the put() and get() functions?


Write a wrapper class upon the HashMap Class, and override the ger() and put() methods.

However, what if we have multiple instance of hashmap? Solution is to use a static variable to count. Some answers are found here.

Good question this is!


public static class MyHashMap<K, V> extends HashMap<K, V> {

    private static final long serialVersionUID = 1L;
    private static int count = 0;

    public V put(K key, V value) {
        return super.put(key, value);

    public V get(Object key) {
        return super.get(key);

    public static int getCount() {
        return count;