简体   繁体   English

哈希对 HashSet 是如何工作的?

[英]How does Hashing work for HashSet?

I have just read and understood how hashing works with HashMap: how it uses a hashtable, makes hashcodes with a hash function etc.我刚刚阅读并理解了哈希如何与 HashMap 一起工作:它如何使用哈希表,使用哈希函数制作哈希码等。

My question is: If the same principles apply to HashSet?我的问题是:如果同样的原则适用于 HashSet? Well obviously it cannot compute a hashcode from HashSet's key, because it doesn't have one.显然它无法从 HashSet 的键计算哈希码,因为它没有。 But does it compute the hashcode from the value of the HashSet then?但是它会根据 HashSet 的值计算哈希码吗?

Actually, a HashSet is backed by HashMap that maps all its keys to a single constant object.实际上, HashSetHashMap支持,它将其所有键映射到单个常量对象。 The key-set of this map acts as the set.此映射的键集充当集合。 See for yourself by looking at the HashSet source .通过查看HashSet源代码亲自查看。 So yes, the mechanisms by which the two operate are very similar.所以是的,两者运作的机制非常相似。

Iс the same principles for HashSet's? HashSet 的原理是否相同?

Yes, HashSet uses the same exact general principle: the keys inserted into the set are arranged according to their hash codes, using the equals method to resolve collisions.是的, HashSet使用完全相同的一般原则:插入到集合中的键根据它们的哈希码排列,使用equals方法解决冲突。

Obviously, it cannot compute a hashcode from HashSet 's key, because it doesn't have one显然,它不能从HashSet的键计算哈希码,因为它没有一个

Of course it has, and it can!当然有,而且可以! In fact, hash keys is all the HashSet has -- just keys, no values.事实上,哈希键是HashSet全部——只有键,没有值。

In a HashMap the "value" is passive: that's something stored at the key;HashMap ,“值”是被动的:它是存储在键中的东西; hash maps never look at values;哈希映射从不查看值; in particular, maps never check values for equality, or compute their hash code.特别是,地图从不检查值是否相等,或计算它们的哈希码。 You can think of a hash set as a hash map that maps everything to null .您可以将散列集视为将所有内容映射到null的散列映射。

HashSet uses HashMap internally to store it's objects. HashSet 在内部使用 HashMap 来存储它的对象。 Whenever you create a HashSet object, one HashMap object associated with it is also created.每当您创建 HashSet 对象时,也会创建一个与之关联的 HashMap 对象。 This HashMap object is used to store the elements you enter in the HashSet.这个 HashMap 对象用于存储您在 HashSet 中输入的元素。 The elements you add into HashSet are stored as keys of this HashMap object.您添加到 HashSet 中的元素存储为此 HashMap 对象的键。 The value associated with those keys will be a constant.与这些键关联的值将是一个常量。 Whenever you insert an element into HashSet using add() method, it actually creates an entry in the internally backing HashMap object with element you have specified as it's key and constant called “PRESENT” as it's value.每当您使用 add() 方法将一个元素插入到 HashSet 中时,它实际上会在内部支持的 HashMap 对象中创建一个条目,其中包含您指定为键的元素和名为“PRESENT”的常量作为其值。 This “PRESENT” is defined in the HashSet class as below.这个“PRESENT”在HashSet类中定义如下。

// 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;
}

You can notice that, add() method of HashSet class internally calls put() method of backing HashMap object by passing the element you have specified as a key and constant “PRESENT” as it's value.您可以注意到,HashSet 类的 add() 方法通过传递您指定的元素作为键和常量“PRESENT”作为它的值,在内部调用支持 HashMap 对象的 put() 方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM