相关疑难解决方法(0)

HashSet实现中的Null对象

在Java API中,HashSet的实现是使用Object作为内部HashMap的值,

   // Dummy value to associate with an Object in the backing Map
private static final Object PRESENT = new Object();

public boolean add(E e) {
    return map.put(e, PRESENT)==null;
}
Run Code Online (Sandbox Code Playgroud)

但是HashMap允许它的值为null.我认为没有必要填补价值,为什么需要这样做?

java hashset

10
推荐指数
2
解决办法
9125
查看次数

标签 统计

hashset ×1

java ×1