Example usage for java.util WeakHashMap remove

List of usage examples for java.util WeakHashMap remove

Introduction

In this page you can find the example usage for java.util WeakHashMap remove.

Prototype

public V remove(Object key) 

Source Link

Document

Removes the mapping for a key from this weak hash map if it is present.

Usage

From source file:Main.java

public static void main(String[] args) {
    WeakHashMap<String, String> weakHashMap = new WeakHashMap<String, String>();

    System.out.println("Putting values into the Map");
    weakHashMap.put("1", "first");
    weakHashMap.put("2", "two");
    weakHashMap.put("3", "from java2s.com");

    // checking Map
    System.out.println("Map: " + weakHashMap);

    // remove value at key 2      
    System.out.println("Returned value: " + weakHashMap.remove("2"));
    System.out.println("New Map: " + weakHashMap);
}

From source file:com.meltmedia.cadmium.servlets.ClassLoaderLeakPreventor.java

/**
 * Workaround for leak caused by Mojarra JSF implementation if included in the container.
 * See <a href="http://java.net/jira/browse/JAVASERVERFACES-2746">JAVASERVERFACES-2746</a>
 *///from w  w w. j  ava2  s.co m
protected void fixJsfLeak() {
    /*
     Note that since a WeakHashMap is used, it is not the map key that is the problem. However the value is a
     Map with java.beans.PropertyDescriptor as value, and java.beans.PropertyDescriptor has a Hashtable in which
     a class is put with "type" as key. This class may have been loaded by the web application.
            
     One example case is the class org.primefaces.component.menubutton.MenuButton that points to a Map with a key 
     "model" whose PropertyDescriptor.table has key "type" with the class org.primefaces.model.MenuModel as its value.
            
     For performance reasons however, we'll only look at the top level key and remove any that has been loaded by the
     web application.     
     */

    Object o = getStaticFieldValue("javax.faces.component.UIComponentBase", "descriptors");
    if (o instanceof WeakHashMap) {
        WeakHashMap descriptors = (WeakHashMap) o;
        final Set<Class> toRemove = new HashSet<Class>();
        for (Object key : descriptors.keySet()) {
            if (key instanceof Class && isLoadedByWebApplication((Class) key)) {
                // For performance reasons, remove all classes loaded in web application
                toRemove.add((Class) key);

                // This would be more correct, but presumably slower
                /*
                Map<String, PropertyDescriptor> m = (Map<String, PropertyDescriptor>) descriptors.get(key);
                for(Map.Entry<String,PropertyDescriptor> entry : m.entrySet()) {
                  Object type = entry.getValue().getValue("type"); // Key constant javax.el.ELResolver.TYPE
                  if(type instanceof Class && isLoadedByWebApplication((Class)type)) {
                    toRemove.add((Class) key); 
                  }
                }
                */
            }
        }

        if (!toRemove.isEmpty()) {
            info("Removing " + toRemove.size() + " classes from Mojarra descriptors cache");
            for (Class clazz : toRemove) {
                descriptors.remove(clazz);
            }
        }
    }
}