除了HashSet不允许重复值之外,HashMap和HashSet之间还有什么区别呢?

我是说执行方面?这有点模糊,因为两者都使用哈希表来存储值。


当前回答

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

其他回答

HashSet允许我们在集合中存储对象,而HashMap允许我们在键和值的基础上存储对象。每个对象或存储对象都有键。

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

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

HashSet

HashSet class implements the Set interface 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”} HashSet does not allow duplicate elements that mean you can not store duplicate values in HashSet. HashSet permits to have a single null value. 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

HashMap class implements the Map interface 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”} HashMap does not allow duplicate keys however it allows having duplicate values. HashMap permits single null key and any number of null values. 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

更多信息请参考这篇文章。

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

HashSet

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

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.

Hashset内部实现HashMap。如果您看到内部实现,则HashSet中插入的值将存储为HashMap中的键,并且该值是object类的Dummy对象。 HashMap和HashSet的区别是:-

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. 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). HashSet and HashMap do not maintain the order of insertion while iterating.