HashSet 和 HashMap 的区别?

发布于 2024-08-31 05:55:23 字数 129 浏览 5 评论 0原文

除了HashSet不允许重复值之外,HashMapHashSet在实现上还有什么区别呢?

这有点模糊,因为两者都使用哈希表来存储值。

Apart from the fact that HashSet does not allow duplicate values, what is the difference between HashMap and HashSet in their implementation?

It's a little bit vague because both use hash tables to store values.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(20

唯憾梦倾城 2024-09-07 05:55:23

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

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

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

HashSet中不能有重复的元素。

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

HashMap is a key -> value (key to value) map, e.g. {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.

In the HashSet, there must be no duplicate elements.

伏妖词 2024-09-07 05:55:23

它们是完全不同的结构。 HashMapMap 的实现。 Map 将键映射到值。使用哈希进行键查找。

另一方面,HashSetSet 的实现。 Set 旨在匹配数学模型一组。正如您所指出的,HashSet 确实使用 HashMap 来支持其实现。然而,它实现了一个完全不同的接口。

当您正在寻找最适合您的Collection时,这个教程是一个很好的起点。如果您确实想知道发生了什么,也有一本书

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

On the other hand, a HashSet is an implementation of Set. A Set is designed to match the mathematical model of a set. A HashSet does use a HashMap to back its implementation, as you noted. However, it implements an entirely different interface.

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

甜柠檬 2024-09-07 05:55:23

HashSet

  1. HashSet类实现了Set接口
  2. 在HashSet中,我们存储对象(元素或值)
    例如,如果我们有一个字符串元素的 HashSet,那么它可以描述一个
    HashSet 元素集合:{“Hello”、“Hi”、“Bye”、“Run”}
  3. HashSet 不允许重复元素,这意味着您
    HashSet 中不能存储重复值。
  4. HashSet 允许有一个空值。
  5. HashSet 不是同步的,这意味着除非显式同步,否则它们不适合线程安全操作。[相似性]

     add 包含下一个注释
    HashSet O(1) O(1) O(h/n) h 是表 
    

HashMap

  1. HashMap 类实现了 Map 接口
  2. HashMap 是
    用于存储密钥和值对。简而言之,它保持了
    键和键的映射value(HashMap类大致相当于
    哈希表,除了它是不同步的并且允许空值。)
    如果 HashMap 元素具有整数键,那么它就是如何表示它的
    String类型的值:例如{1->”Hello”, 2->”Hi”, 3->”Bye”,
    4->“Run”}
  3. HashMap 不允许重复的键,但它允许有重复的值。
  4. HashMap 允许单个空键和任意数量的空值。
  5. HashMap 不同步,这意味着除非显式同步,否则它们不适合线程安全操作。[相似性]

     获取 containsKey 下一个注释
     HashMap O(1) O(1) O(h/n) h 是表 
    

请参阅 本文 查找更多信息。

HashSet

  1. HashSet class implements the Set interface
  2. In HashSet, we store objects(elements or values)
    e.g. If we have a HashSet of string elements then it could depict a
    set of HashSet elements: {“Hello”, “Hi”, “Bye”, “Run”}
  3. HashSet does not allow duplicate elements that mean you
    can not store duplicate values in HashSet.
  4. HashSet permits to have a single null value.
  5. HashSet is not synchronized which means they are not suitable for thread-safe operations until unless synchronized explicitly.[similarity]

                          add      contains next     notes
    HashSet               O(1)     O(1)     O(h/n)   h is the table 
    

HashMap

  1. HashMap class implements the Map interface
  2. HashMap is
    used for storing key & value pairs. In short, it maintains the
    mapping of key & value (The HashMap class is roughly equivalent to
    Hashtable, except that it is unsynchronized and permits nulls.) This
    is how you could represent HashMap elements if it has integer key
    and value of String type: e.g. {1->”Hello”, 2->”Hi”, 3->”Bye”,
    4->”Run”}
  3. HashMap does not allow duplicate keys however it allows having duplicate values.
  4. HashMap permits single null key and any number of null values.
  5. HashMap is not synchronized which means they are not suitable for thread-safe operations until unless synchronized explicitly.[similarity]

                           get      containsKey next     Notes
     HashMap               O(1)     O(1)        O(h/n)   h is the table 
    

Please refer this article to find more information.

电影里的梦 2024-09-07 05:55:23

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

不要对他们的名字感到困惑;它们是截然不同的东西。

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 Set and 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.

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

猫卆 2024-09-07 05:55:23

Hashset 内部实现了HashMap。如果您看到内部实现,则插入到 HashSet 中的值将被存储作为 HashMap 中的键,值是 Object 类的虚拟对象。
HashMap 与 HashSet 之间的区别是: -

  1. HashMap 包含键值对,每个值都可以通过键访问,而 HashSet 需要每次迭代,因为没有 get 方法。
  2. HashMap 实现 Map 接口,允许一个 null 值作为 key,多个 null 值作为 value,而 HashSet 实现 Set 接口,只允许一个 null 值,不允许重复值。(请记住,HashMap 键中允许有一个空键,因此 HashSet 中允许有一个空值,因为 HashSet 在内部实现了 HashMap)。
  3. HashSetHashMap 在迭代时不保持插入顺序。

The Hashset Internally implements HashMap. If you see the internal implementation the values inserted in HashSet are stored as keys in the HashMap and the value is a Dummy object of Object class.
Difference between HashMap vs HashSet is:-

  1. HashMap contains key value pairs and each value can be accessed by key where as HashSet needs to be iterated everytime as there is no get method.
  2. HashMap implements Map interface and allows one null value as a key and multiple null values as values, whereas HashSet implements Set interface, allows only one null value and no duplicated values.(Remeber one null key is allowed in HashMap key hence one null value in HashSet as HashSet implemements HashMap internally).
  3. HashSet and HashMap do not maintain the order of insertion while iterating.
野侃 2024-09-07 05:55:23

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

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.

一瞬间的火花 2024-09-07 05:55:23

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

As the names imply, a HashMap is an associative Map (mapping from a key to a value), a HashSet is just a Set.

仙女 2024-09-07 05:55:23

HashMap 用于添加、获取、删除...由任何类型的自定义键索引的对象。
HashSet 用于添加元素、删除元素以及通过比较元素的哈希值来检查元素是否存在。

因此,HashMap 包含元素,而 HashSet 则记住它们的哈希值。

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

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

坚持沉默 2024-09-07 05:55:23

Java中HashSet和HashMap的区别

1) HashMap和HashSet之间第一个也是最显着的区别是HashMap是Map接口的实现,而HashSet是Set接口的实现,这意味着 HashMap 是基于键值的数据结构,而 HashSet 通过不允许重复来保证唯一性。实际上,HashSet 是 Java 中 HashMap 的包装器,如果你查看 HashSet.java 的 add(E e) 方法的代码,你会发现请参阅以下代码:

public boolean add(E e) 
{
    return map.put(e, PRESENT)==null;
}

其中将对象作为键和值放入映射中是一个最终对象 PRESENT,它是虚拟的。

2) HashMap 和 HashSet 之间的第二个区别是,我们使用 add() 方法将元素放入 Set 中,而在 Java 中我们使用 put() 方法将键和值插入到 HashMap 中。

3) HashSet 只允许有一个 null key,但是 HashMap 可以允许一个 null key + 多个 null value。

这就是Java中HashSet和HashMap之间的区别。总之,HashSet 和 HashMap 是两种不同类型的 Collection,一种是 Set,另一种是 Map。

Differences between HashSet and HashMap in Java

1) First and most significant difference between HashMap and HashSet is that HashMap is an implementation of Map interface while HashSet is an implementation of Set interface, which means HashMap is a key value based data-structure and HashSet guarantees uniqueness by not allowing duplicates.In reality HashSet is a wrapper around HashMap in Java, if you look at the code of add(E e) method of HashSet.java you will see following code :

public boolean add(E e) 
{
    return map.put(e, PRESENT)==null;
}

where its putting Object into map as key and value is an final object PRESENT which is dummy.

2) Second difference between HashMap and HashSet is that , we use add() method to put elements into Set but we use put() method to insert key and value into HashMap in Java.

3) HashSet allows only one null key, but HashMap can allow one null key + multiple null values.

That's all on difference between HashSet and HashMap in Java. In summary HashSet and HashMap are two different type of Collection one being Set and other being Map.

尤怨 2024-09-07 05:55:23

Java中HashSet和HashMap的区别

HashSet内部使用HashMap来存储对象。当调用add(String)方法时,它会调用HahsMap put(key,value)方法,其中key=String对象与value=new Object(Dummy)。因此它不保留重复项,因为键只是值对象。

在 Hashset/HashMap 中作为键存储的对象应该覆盖 hashcode &等于合同。

用于访问/存储 HashMap 中值对象的键应声明为 Final,因为当它被修改时,无法定位和存储值对象。返回空值。

Differences between HashSet and HashMap in Java

HashSet internally uses HashMap to store objects.when add(String) method called it calls HahsMap put(key,value) method where key=String object & value=new Object(Dummy).so it maintain no duplicates because keys are nothing but Value Object.

the Objects which are stored as key in Hashset/HashMap should override hashcode & equals contract.

Keys which are used to access/store value objects in HashMap should declared as Final because when it is modified Value object can't be located & returns null.

酒中人 2024-09-07 05:55:23

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

编辑:针对马修的评论,他是对的;我把它搞反了。内部 HashMap 使用构成 Set 元素的对象作为键。 HashMap 的值是一个对象,只是简单地存储在 HashMap 存储桶中。

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.

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.

过期情话 2024-09-07 05:55:23

差异:
关于继承权:
HashSet 实现了 Set。
HashMap 实现 Map 并存储键和值的映射。

HashSet 和 HashMap 在数据库中的使用将帮助您理解各自的重要性。

HashSet:一般用于存储唯一的集合对象。
例如:它可以用作存储
之间的多对一关系的实现类
类别项目和类别出价,其中(项目有许多出价)
HashMap:用于将键映射到值。值可以为 null 或任何对象/对象列表(本身就是对象)。

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

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).

残月升风 2024-09-07 05:55:23

一个 HashSet 是根据 HashMap.它是键和 PRESENT 对象之间的映射。

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

笨死的猪 2024-09-07 05:55:23

HashMap 是一个 Map 实现,允许重复值,但不允许重复键。。要添加对象,需要键/值对。允许空键和空值。例如:

{The->3,world->5,is->2,nice->4}

HashSet 是一个 Set 实现,它不允许重复。如果您尝试添加重复的对象,调用 public boolean add(Object o) 方法,则集合保持不变并返回 false。例如:

[世界很好]

HashMap is a Map implementation, allowing duplicate values but not duplicate keys.. For adding an object a Key/Value pair is required. Null Keys and Null values are allowed. eg:

{The->3,world->5,is->2,nice->4}

HashSet is a Set implementation,which does not allow duplicates.If you tried to add a duplicate object, a call to public boolean add(Object o) method, then the set remains unchanged and returns false. eg:

[The,world,is,nice]

带上头具痛哭 2024-09-07 05:55:23

基本上,在 HashMap 中,用户必须同时提供 Key 和 Value,而在 HashSet 中,您只需提供 Value,Key 是通过使用哈希函数自动从 Value 派生的。所以有了Key和Value之后,HashSet内部就可以存储为HashMap了。

Basically in HashMap, user has to provide both Key and Value, whereas in HashSet you provide only Value, the Key is derived automatically from Value by using hash function. So after having both Key and Value, HashSet can be stored as HashMap internally.

拔了角的鹿 2024-09-07 05:55:23

HashSet和HashMap都是存储pairs,区别在于HashMap中可以指定key,而HashSet中key来自对象的哈希码

HashSet and HashMap both store pairs , the difference lies that in HashMap you can specify a key while in HashSet the key comes from object's hash code

鸢与 2024-09-07 05:55:23

HashMap 允许一个空键和一个空值。它们不同步,这提高了效率。如果需要,您可以使用Collections.SynchronizedMap()使它们同步。

Hashtables不允许空键并且是同步的。

HashMaps allow one null key and null values. They are not synchronized, which increases efficiency. If it is required, you can make them synchronized using Collections.SynchronizedMap()

Hashtables don't allow null keys and are synchronized.

屋顶上的小猫咪 2024-09-07 05:55:23

它们之间的主要区别如下:

HashSet

  • 它不允许重复的键。
  • 即使不同步,这样也会有更好的性能。
  • 它允许空键。
  • 当您想要维护唯一列表时可以使用 HashSet。
  • HashSet 实现了 Set 接口,它由哈希表(实际上是 HashMap 实例)支持。
  • HashSet 存储对象。
  • HashSet 不允许重复元素,但允许空值。
  • 此接口不保证顺序随着时间的推移保持不变。

HashMap

  • 它允许重复的键。
    它不是同步的,因此这会有更好的性能。
  • HashMap 不维护插入顺序。
  • 该顺序由哈希函数定义。
  • 它不是线程安全的
  • 它允许键和值都为 null。
  • 它允许一个空键和任意数量的空值。
  • HashMap 是 Map 接口的基于哈希表的实现。
  • HashMap 将对象存储为键和值对。
  • HashMap 不允许重复键,但允许空键和值。
  • 不保证超时时元素的排序。

The main difference between them you can find as follows:

HashSet

  • It does not allow duplicate keys.
  • Even it is not synchronized, so this will have better performance.
  • It allows a null key.
  • HashSet can be used when you want to maintain a unique list.
  • HashSet implements Set interface and it is backed by the hash table(actually HashMap instance).
  • HashSet stores objects.
  • HashSet doesn’t allow duplicate elements but null values are allowed.
  • This interface doesn’t guarantee that order will remain constant over time.

HashMap

  • It allows duplicate keys.
    It is not synchronized, so this will have better performance.
  • HashMap does not maintain insertion order.
  • The order is defined by the Hash function.
  • It is not Thread Safe
  • It allows null for both key and value.
  • It allows one null key and as many null values as you like.
  • HashMap is a Hash table-based implementation of the Map interface.
  • HashMap store object as key and value pair.
  • HashMap does not allow duplicate keys but null keys and values are allowed.
  • Ordering of the element is not guaranteed overtime.
万人眼中万个我 2024-09-07 05:55:23

编辑 - 这个答案不正确。我将其留在这里以防其他人有类似的想法。 b.roth 和 justkt 上面有正确答案。

--- 原始 ---

你几乎回答了你自己的问题 - 哈希集不允许重复值。使用支持哈希图构建哈希集是微不足道的(只需检查该值是否已存在)。我猜想各种java实现要么做到这一点,要么实现一些自定义代码以更有效地做到这一点。

EDIT - this answer isn't correct. I'm leaving it here in case other people have a similar idea. b.roth and justkt have the correct answers above.

--- original ---

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.

浮华 2024-09-07 05:55:23

HashMap是Map接口的实现
HashSet是Set接口HashMap的实现,

以键值对的形式存储数据
HashSet 仅存储对象

Put 方法用于在映射中添加元素
Add方法用于添加元素为Set

在hashmap中使用key对象计算hashcode值
这里成员对象用于计算两个对象的哈希码值可以相同,因此 equal() 方法用于检查是否相等,如果返回 false 则意味着两个对象不同。

HashMap 比 hashset 更快,因为使用唯一键来访问对象
HashSet 比 Hashmap 慢

HashMap is a implementation of Map interface
HashSet is an implementation of Set Interface

HashMap Stores data in form of key value pair
HashSet Store only objects

Put method is used to add element in map
Add method is used to add element is Set

In hash map hashcode value is calculated using key object
Here member object is used for calculating hashcode value which can be same for two objects so equal () method is used to check for equality if it returns false that means two objects are different.

HashMap is faster than hashset because unique key is used to access object
HashSet is slower than Hashmap

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文