I was asked in an interview to suggest a design/implementation of a Singleton Pattern where I have to Lazy load the class and also not use the synchronized key word. I got choked and could not come up with anything.I then I started reading on java concurrency and concurrentHaspMap. Please check the below imlpementation and kindly confirm if you see any issue with Double check Locking or any other issues with this implementation.
package Singleton;
import java.util.concurrent.ConcurrentHashMap;
public final class SingletonMap {
static String key = "SingletonMap";
static ConcurrentHashMap<String, SingletonMap> singletonMap = new ConcurrentHashMap<String, SingletonMap>();
//private constructor
private SingletonMap(){
}
static SingletonMap getInstance(){
SingletonMap map = singletonMap.get(key);
if (map == null){
//SingletonMap newValue= new SingletonMap();
map = singletonMap.putIfAbsent(key,new SingletonMap());
if(map == null){
map = singletonMap.get(key);
}
}
return map;
}
}