Java HashSet 和 HashMap 的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2773824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 12:42:27  来源:igfitidea点击:

Difference between HashSet and HashMap?

javacollectionshashmaphashset

提问by SpikETidE

Apart from the fact that HashSetdoes not allow duplicate values, what is the difference between HashMapand HashSet?

除了HashSet不允许重复值这一事实外,HashMap和之间有什么区别HashSet

I mean implementation wise? It's a little bit vague because both use hash tablesto store values.

我的意思是实施明智?这有点含糊,因为两者都使用哈希表来存储值。

采纳答案by justkt

They are entirely different constructs. A HashMapis an implementation of Map. A Mapmaps keys to values. The key look up occurs using the hash.

它们是完全不同的结构。AHashMap是 的实现Map。一个地图键映射到值。使用散列进行键查找。

On the other hand, a HashSetis an implementation of Set. A Setis designed to match the mathematical model of a set. A HashSetdoes use a HashMapto back its implementation, as you noted. However, it implements an entirely different interface.

另一方面, aHashSet是 的实现Set。一被设计成一组的数学模型相匹配。正如您所指出的,AHashSet确实使用 aHashMap来支持其实现。但是,它实现了一个完全不同的接口。

When you are looking for what will be the best Collectionfor your purposes, this Tutorialis a good starting place. If you truly want to know what's going on, there's a book for that, too.

当您正在寻找最Collection适合您的目的时,本教程是一个很好的起点。如果你真的想知道发生了什么,也有一本书可以解决这个问题

回答by Matthew Flaschen

A HashSetis implemented in terms of a HashMap. It's a mapping between the key and a PRESENT object.

一个HashSet的是在方面实现HashMap中。它是键和 PRESENT 对象之间的映射。

回答by chris

you pretty much answered your own question - hashset doesn't allow duplicate values. it would be trivial to build a hashset using a backing hashmap (and just a check to see if the value already exists). i guess the various java implementations either do that, or implement some custom code to do it more efficiently.

你几乎回答了你自己的问题——hashset 不允许重复值。使用后备哈希映射构建哈希集将是微不足道的(并且只是检查该值是否已经存在)。我猜各种 java 实现要么这样做,要么实现一些自定义代码以更有效地做到这一点。

回答by Andy Gherna

A HashSet uses a HashMap internally to store its entries. Each entry in the internal HashMap is keyed by a single Object, so all entries hash into the same bucket. I don't recall what the internal HashMap uses to store its values, but it doesn't really matter since that internal container will never contain duplicate values.

HashSet 在内部使用 HashMap 来存储其条目。内部 HashMap 中的每个条目都以单个对象为键,因此所有条目都散列到同一个桶中。我不记得内部 HashMap 使用什么来存储其值,但这并不重要,因为该内部容器永远不会包含重复值。

EDIT: To address Matthew's comment, he's right; I had it backwards. The internal HashMap is keyed with the Objects that make up the Set elements. The values of the HashMap are an Object that's just simply stored in the HashMap buckets.

编辑:为了解决马修的评论,他是对的;我把它倒过来了。内部 HashMap以构成 Set 元素的 Object为键。HashMap 的值是一个对象,它只是简单地存储在 HashMap 存储桶中。

回答by Spidfire

HashSet allows us to store objects in the set where as HashMap allows us to store objects on the basis of key and value. Every object or stored object will be having key.

HashSet 允许我们在集合中存储对象,而 HashMap 允许我们根据键和值存储对象。每个对象或存储的对象都将具有密钥。

回答by leonbloy

As the names imply, a HashMapis an associative Map(mapping from a key to a value), a HashSetis just a Set.

顾名思义,HashMap是一个关联Map(从键到值的映射),HashSet只是一个Set

回答by b.roth

HashSet is a set, e.g. {1,2,3,4,5}

HashSet 是一个集合,例如{1,2,3,4,5}

HashMap is a key -> value(key to value) map, e.g. {a -> 1, b -> 2, c -> 2, d -> 1}

HashMap 是一个键 -> 值(键到值)映射,例如{a -> 1, b -> 2, c -> 2, d -> 1}

Notice in my example above that in the HashMap there must not be duplicate keys, but it may have duplicate values.

请注意,在我上面的示例中,HashMap 中不能有重复的键,但可以有重复的值。

In the HashSet, there must be no duplicate elements.

在 HashSet 中,不能有重复的元素。

回答by Martijn Courteaux

A HashMapis to add, get, remove, ... objects indexed by a custom key of any type.
A HashSetis to add elements, remove elements and check if elements are present by comparing their hashes.

AHashMap是添加、获取、删除、...由任何类型的自定义键索引的对象。
AHashSet是添加元素,删除元素并通过比较它们的散列来检查元素是否存在。

So a HashMap contains the elements and a HashSet remembers their hashes.

所以 HashMap 包含元素,而 HashSet 记住它们的哈希值。

回答by frictionlesspulley

Differences: with respect to heirarchy: HashSet implements Set. HashMap implements Map and stores a mapping of keys and values.

区别:关于层次结构:HashSet 实现了 Set。HashMap 实现 Map 并存储键和值的映射。

A use of HashSet and HashMap with respect to database would help you understand the significance of each.
HashSet:is generally used for storing unique collection objects. E.g: It might be used as implementation class for storing many-to-one relation ship between
class Item and Class Bid where (Item has many Bids)HashMap:is used to map a key to value.the value may be null or any Object /list of Object (which is object in itself).

在数据库方面使用 HashSet 和 HashMap 将帮助您理解每个的重要性。
HashSet:一般用于存储唯一的集合对象。例如:它可以用作存储
类 Item 和 Class Bid之间的多对一关系的实现类,其中(Item 有许多 Bid)HashMap:用于将键映射到值。值可以是 null 或任何对象/ 对象列表(它本身就是对象)。

回答by Carl Manaster

It's really a shame that both their names start with Hash. That's the least important part of them. The important parts come after the Hash- the Setand Map, as others have pointed out. What they are, respectively, are a Set- an unordered collection - and a Map- a collection with keyed access. They happen to be implemented with hashes - that's where the names come from - but their essence is hidden behind that part of their names.

很遗憾他们的名字都以Hash开头。这是其中最不重要的部分。正如其他人指出的那样,重要的部分在Hash 之后- SetMap。它们分别是一个Set- 一个无序集合 - 和一个Map- 一个具有键控访问的集合。它们碰巧是用散列实现的——这就是名称的来源——但它们的本质隐藏在它们名称的那部分后面。

Don't be confused by their names; they are deeply different things.

不要被他们的名字混淆;它们是非常不同的东西。