Code example for ConcurrentHashMap

Methods: getputIfAbsent

public class UgiFactory { 
    private static ConcurrentHashMap<String, UserGroupInformation> userUgiMap = 
            new ConcurrentHashMap<String, UserGroupInformation>(); 
    static UserGroupInformation getUgi(String user) throws IOException{ 
        UserGroupInformation ugi = userUgiMap.get(user); 
        if(ugi == null){ 
            //create new ugi and add to map 
            final UserGroupInformation newUgi =  
            //if another thread adds an entry before the check in this one 
            // the one created here will not be added. 
            userUgiMap.putIfAbsent(user, newUgi); 
            //use the UGI object that got added 
            return userUgiMap.get(user); 
        return ugi;