Created
August 15, 2012 22:35
-
-
Save jirwin/3364328 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
public class DefaultDict<K, V> extends HashMap<K, V> { | |
Class<V> klass; | |
public DefaultDict(Class klass) { | |
this.klass = klass; | |
} | |
@Override | |
public V get(Object key) { | |
V returnValue = super.get(key); | |
if (returnValue == null) { | |
try { | |
returnValue = klass.newInstance(); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
this.put((K) key, returnValue); | |
} | |
return returnValue; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment