Example usage for java.util WeakHashMap WeakHashMap

List of usage examples for java.util WeakHashMap WeakHashMap

Introduction

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

Prototype

public WeakHashMap() 

Source Link

Document

Constructs a new, empty WeakHashMap with the default initial capacity (16) and load factor (0.75).

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Map weakMap = new WeakHashMap();
    Object keyObject = "";
    Object valueObject = "";
    weakMap.put(keyObject, valueObject);

    Iterator it = weakMap.keySet().iterator();
    while (it.hasNext()) {
        Object key = it.next();//from   w  w  w.  j a v a  2  s.com
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    map = new WeakHashMap();
    map.put("A", "B");
    Runnable runner = new Runnable() {
        public void run() {
            while (map.containsKey("A")) {
                try {
                    Thread.sleep(1000);
                    System.gc();//from   www .j av  a 2s.  c o m
                } catch (InterruptedException ignored) {
                }
                System.out.println("Has A");
                System.gc();
            }
        }
    };
    Thread t = new Thread(runner);
    t.start();
    System.out.println("Main waiting");
    try {
        t.join();
    } catch (InterruptedException ignored) {
    }
    System.gc();
}

From source file:Main.java

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

    System.out.println("Populating two Maps");

    weakHashMapOne.put("1", "first");
    weakHashMapOne.put("2", "two");
    weakHashMapOne.put("3", "from java2s.com");

    weakHashMapTwo.put("1", "1st");
    weakHashMapTwo.put("2", "2nd");
    weakHashMapTwo.put("3", "3rd");

    // checking Map
    System.out.println("Before - Map 1: " + weakHashMapOne);
    System.out.println("Before - Map 2: " + weakHashMapTwo);

    // putting map 2 into map1
    weakHashMapOne.putAll(weakHashMapTwo);

    System.out.println("After - Map 1: " + weakHashMapOne);
    System.out.println("After - Map 2: " + weakHashMapTwo);
}

From source file:Main.java

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

    weakHashMap.put("1", "first");
    weakHashMap.put("2", "two");
    weakHashMap.put("3", "from java2s.com");

    // printing the contents of the Map
    System.out.println("Contents of the Map");
    System.out.println(weakHashMap);

    // populating the set
    Set set = weakHashMap.keySet();
    System.out.println("Contents of the set");
    System.out.println(set);//from   w  ww  . j a  v  a 2  s . com
}

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 the size of the Map
    System.out.println("Map values: " + weakHashMap);

    Collection col = weakHashMap.values();
    System.out.println("Collection values: " + col);
}

From source file:Main.java

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

    weakHashMap.put("1", "first");
    weakHashMap.put("2", "two");
    weakHashMap.put("3", "from java2s.com");

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

    // returns a Set view of the Map
    Set set = weakHashMap.entrySet();

    // printing the set
    System.out.println("Set: " + set);

    // change Map value
    weakHashMap.put("3", "new value");

    // print Map and set
    System.out.println("Changed Map: " + weakHashMap);
    System.out.println("Changed Set: " + set);

}

From source file:Main.java

public static void main(String args[]) {

    Map<String, Boolean> map = new WeakHashMap<String, Boolean>();

    // create a set from map
    Set<String> set = Collections.newSetFromMap(map);

    // add values in set
    set.add("Java");
    set.add("C");
    set.add("C++");
    set.add("from java2s.com");

    // set and map values are
    System.out.println("Set is: " + set);
    System.out.println("Map is: " + map);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Object keyObject = "";
    Object valueObject = "";
    Map weakMap = new WeakHashMap();

    weakMap.put(keyObject, valueObject);
    WeakReference weakValue = new WeakReference(valueObject);

    weakMap.put(keyObject, weakValue);/*from ww  w.j a va  2  s  . com*/

    Iterator it = weakMap.keySet().iterator();
    while (it.hasNext()) {
        Object key = it.next();
        weakValue = (WeakReference) weakMap.get(key);
        if (weakValue == null) {
            System.out.println("Value has been garbage-collected");
        } else {
            System.out.println("Get value");
            valueObject = weakValue.get();
        }
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Object keyObject = "";
    Object valueObject = "";
    Map<Object, Object> weakMap = new WeakHashMap<Object, Object>();

    weakMap.put(keyObject, valueObject);
    WeakReference weakValue = new WeakReference<Object>(valueObject);

    weakMap.put(keyObject, weakValue);/*  ww  w  .  ja  v  a  2 s.c o  m*/

    Iterator it = weakMap.keySet().iterator();
    while (it.hasNext()) {
        Object key = it.next();
        weakValue = (WeakReference) weakMap.get(key);
        if (weakValue == null) {
            System.out.println("Value has been garbage-collected");
        } else {
            System.out.println("Get value");
            valueObject = weakValue.get();
        }
    }
}

From source file:Main.java

public static <K, V> WeakHashMap<K, V> createWeakHashMap() {
    return new WeakHashMap<K, V>();
}