Code example for ConcurrentHashMap

 * Type-safe instance map. 
 * @author Kohsuke Kawaguchi 
public class Lookup { 
    private final ConcurrentHashMap<Class,Object> data = new ConcurrentHashMap<Class,Object>();
    public <T> T get(Class<T> type) {
        return type.cast(data.get(type));
    public <T> T set(Class<T> type, T instance) {
        return type.cast(data.put(type,instance));
     * Overwrites the value only if the current value is null. 
     * @return 
     *      If the value was null, return the {@code instance} value. 
Stop searching for code, let great code find you!  Add Codota to your java IDE