Skip to content

Instantly share code, notes, and snippets.

@dsedivec
Created January 21, 2010 19:15
Show Gist options
  • Save dsedivec/283094 to your computer and use it in GitHub Desktop.
Save dsedivec/283094 to your computer and use it in GitHub Desktop.
import java.util.HashMap;
public class EmptyCollectionMap extends HashMap {
Class collectionType;
public EmptyCollectionMap(Class collectionType) {
super();
this.collectionType = collectionType;
}
public Object get(Object key) {
Object collection;
if (containsKey(key))
collection = super.get(key);
else {
try {
collection = collectionType.newInstance();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
put(key, collection);
}
return collection;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment