WeakHashMap和HashMap [英] WeakHashMap vs HashMap

查看:111
本文介绍了WeakHashMap和HashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码示例中,将键设置为null并调用System.gc()时,WeakHashMap会丢失所有映射并被清空.

In the following code example when keys are set to null and System.gc() is called, the WeakHashMap loses all mappings and is emptied.

class WeakHashMapExample {

public static void main(String[] args) {

    Key k1 = new Key("Hello");
    Key k2 = new Key("World");
    Key k3 = new Key("Java");
    Key k4 = new Key("Programming");

    Map<Key, String> wm = new WeakHashMap<Key, String>();


    wm.put(k1, "Hello");
    wm.put(k2, "World");
    wm.put(k3, "Java");
    wm.put(k4, "Programming");
    k1=null;
    k2=null;
    k3=null;
    k4=null;
    System.gc();
    System.out.println("Weak Hash Map :"+wm.toString());

}

}

class Key{

private String key;

public Key(String key) {
    this.key=key;
}

@Override
public boolean equals(Object obj) {
    return this.key.equals((String)obj);
}
@Override
public int hashCode() {
    return key.hashCode();
}
@Override
public String toString() {
    return key;
}

}

Output: Weak Hash Map :{}

WeakHashMapHashMap一起使用并且键设置为null时,WeakHashMap不会丢失其键-值映射.

When WeakHashMap is used along with HashMap and keys are set to null, the WeakHashMap doesn't lose its key-value mappings.

class WeakHashMapExample {

public static void main(String[] args) {

    Key k1 = new Key("Hello");
    Key k2 = new Key("World");
    Key k3 = new Key("Java");
    Key k4 = new Key("Programming");

    Map<Key, String> wm = new WeakHashMap<Key, String>();
    Map<Key, String> hm=new HashMap<Key, String>();

    wm.put(k1, "Hello");
    wm.put(k2, "World");
    wm.put(k3, "Java");
    wm.put(k4, "Programming");

    hm.put(k1, "Hello");
    hm.put(k2, "World");
    hm.put(k3, "Java");
    hm.put(k4, "Programming");
    k1=null;
    k2=null;
    k3=null;
    k4=null;
    System.gc();
    System.out.println("Weak Hash Map :"+wm.toString());
    System.out.println("Hash Map :"+hm.toString());
}

}

class Key{

private String key;

public Key(String key) {
    this.key=key;
}

@Override
public boolean equals(Object obj) {
    return this.key.equals((String)obj);
}
@Override
public int hashCode() {
    return key.hashCode();
}
@Override
public String toString() {
    return key;
}

}

输出: Weak Hash Map :{Java=Java, Hello=Hello, World=World, Programming=Programming} Hash Map :{Programming=Programming, World=World, Java=Java, Hello=Hello}

Output: Weak Hash Map :{Java=Java, Hello=Hello, World=World, Programming=Programming} Hash Map :{Programming=Programming, World=World, Java=Java, Hello=Hello}

我的问题是,即使丢弃了键,为什么WeakHashMap在第二个代码示例中也不会丢失其条目?

My question is why doesn't the WeakHashMap lose its entries in the second code example even after the keys are discarded?

推荐答案

A HashMap 维护对键的硬引用,键仍然可以访问,并且WeakHashMap不会丢弃条目.

A WeakHashMap discards entries when the key is no longer strongly reachable from live code. Since the HashMap maintains a hard reference to the keys, the keys are still reachable and the WeakHashMap doesn't discard the entries.

重点是,行为与对键对象的引用有关,而不是与一次引用键的任何变量的值有关.

The point is that the behavior has to do with references to the key objects, not to the value of any variable that might have at one time had a reference to the keys.

这篇关于WeakHashMap和HashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆