简体   繁体   English

为什么需要迭代Java中的HashSet?

[英]Why does a HashSet in Java need to be iterated?

I understand that in Java, the HashMap class implements the Map interface and the HashSet class implements the Set interface. 我了解在Java中,HashMap类实现Map接口,而HashSet类实现Set接口。

I also understand that both those classes are implementations of a hash table. 我也明白这两个类都是哈希表的实现。

That said, why is it necessary to iterate through a HashSet while a HashMap has a get() method? 就是说,为什么在HashMap具有get()方法时有必要遍历HashSet? Isn't the point of having a hash table being able to fetch an element through a value associated to it? 散列表是否能够通过与之关联的值来获取元素的意义不在于此?

I've looked up the docs and done some searching and could not find an answer. 我查了一下文档,做了一些搜索,找不到答案。

Thank you in advance. 先感谢您。

A get() method of HashSet (or any Set implementation) would be redundant, since if set.contains(x) returns true , you already have a reference ( x ) to an instance which is deemed equal to the corresponding element of the Set . HashSet (或任何Set实现)的get()方法将是多余的,因为如果set.contains(x)返回true ,则您已经具有对实例的引用( x ),该实例被视为等于Set的相应元素。

In the Map interface you need map.get(key) in order to obtain the value associated with the key. Map界面中,您需要map.get(key)来获取与键关联的值。 In the Set interface, there is only the key, with no (meaningful) value associated to it. Set界面中,只有键,没有与之关联的(有意义的)值。

I also understand that both those classes are implementations of a hash table. 我也明白这两个类都是哈希表的实现。

If by that statement you meant that HashSet is implemented with a backing HashMap instance, that's just an implementation detail. 如果通过该语句表示HashSet是通过支持HashMap实例实现的,那么这只是实现细节。 The values corresponding to the keys of this backing HashMap are all references to the same dummy object. 与此后备HashMap的键相对应的值都是对同一个伪对象的引用。 There's no reason to ever call get() for the backing HashMap . 没有理由调用HashMap get()

I also understand that both those classes are implementations of a hash table. 我也明白这两个类都是哈希表的实现。

I think you're guilty of assuming too much about the implementation. 我认为您对实现的假设过多。 As with all objects, the key is the public interface the designer chooses to expose to you. 与所有对象一样,关键是设计师选择向您公开的公共接口。

The Set interface does not include a get method to access individual objects directly. Set接口不包含直接访问单个对象的get方法。 The semantics are different from Map , by design. 语义在设计上不同于Map

I think there is no get method because, hashset are internally backed by hashMap and all your values of set are actually stored as keys of this HashMap and values is just a constant field called "PRESENT". 我认为没有get方法,因为hashset在内部由hashMap支持,并且您的所有set值实际上都存储为此HashMap的键,而值只是一个称为“ PRESENT”的常量字段。 So if you have a get() it will cause a problem. 因此,如果您有一个get()它将引起问题。

Please refer below link for more details http://www.java67.com/2014/01/how-hashset-is-implemented-or-works-internally-java.html 请参考下面的链接以获取更多详细信息http://www.java67.com/2014/01/how-hashset-is-implemented-or-works-internally-java.html

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

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