213

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

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

S.K.
  • 3,597
  • 2
  • 16
  • 31
SpikETidE
  • 6,711
  • 15
  • 46
  • 62

20 Answers20

349

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.

b.roth
  • 9,421
  • 8
  • 37
  • 50
  • 1
    But the (most interesting) reason for the confusion is that even in HashSet you need a "key" to access the elements. I.e., objects, even in mathematics, have names (or addresses), if they're to be accessed or referenced. So in this real sense a HashSet is an especially simple HashMap, keyed with the names (or addresses) of its elements. – Andrew Marshall Mar 03 '18 at 04:40
184

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.

bwegs
  • 3,769
  • 2
  • 30
  • 33
justkt
  • 14,610
  • 8
  • 42
  • 62
  • That statement is a bit simplistic. There's some more going on under the covers, ""Returns a hash value for the specified object. In addition to the object's own hashCode, this method applies a "supplemental hash function," which defends against _poor_ quality hash functions. This is critical because HashMap uses power-of two length hash tables." http://weblogs.java.net/blog/2005/06/18/hashmap-implementation - however, if you look at the doc you'll see that this hash distributes things over "buckets", so in the end I believe two things can get mapped the same bucket. – justkt May 05 '10 at 14:08
  • 1
    To answer your second question - no. A map is if you want (key -> value) as defined by @Bruno Rothgiesser's excellent answer. A set is for non-duplicate elements. If you want duplicates and not key->value, I'd check out a java.util.List implementation. Check out the Collection tutorial for a definitive guide: http://java.sun.com/docs/books/tutorial/collections/index.html – justkt May 05 '10 at 14:10
  • @justk: yes, you can get two keys in one bucket, and then equals() is used to distinguish between them. That's why it's essential that hashCode() and equals() are compatible. – Michael Borgwardt May 05 '10 at 14:14
  • 6
    @SpikETidE: neither HashMap nor HashSet allow duplicates. That's the whole point. – Michael Borgwardt May 05 '10 at 14:15
  • 28
    @SpikETidE: a set doesn't have key/value pairs, only elements. And HashSet is implemented by having a HashMap with the set elements as keys and the value being ignored. – Michael Borgwardt May 05 '10 at 14:20
77

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.

A.K.
  • 2,284
  • 3
  • 26
  • 35
40

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.

Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
  • @HiteshSahu They are both implemented with Hash Tables (https://en.wikipedia.org/wiki/Hash_table). This is a good data structure for representing a set, efficient in the right ways, and, essentially, a HashMap's keys are implemented as a HashSet. So whoever named them went to some trouble to implement them and was focused on the implementation rather than their purpose (at a guess). – Carl Manaster Dec 16 '17 at 16:48
8

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.
Piyush
  • 1,744
  • 1
  • 15
  • 28
Abhay S
  • 91
  • 1
  • 1
5

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.

Spidfire
  • 5,433
  • 6
  • 28
  • 36
5

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

leonbloy
  • 73,180
  • 20
  • 142
  • 190
  • 3
    @SpikETidE That's a detail of how uniqueness is implemented, but the meaning of HashSet is to implement a set. – Michael Borgwardt May 05 '10 at 14:03
  • 1
    so.. it all boils down to "if you don't want duplicates use hashSet... If you don't bother about duplicates use HashMap"....? – SpikETidE May 05 '10 at 14:05
  • 3
    Java does not implement a specific class for a "collection with potentially duplicated elements" (a "bag"), you can use a List for this (though a List adds some semantic to the bag: order; but you can ignore this). – leonbloy May 05 '10 at 14:29
2

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.

Vibha Sanskrityayan
  • 1,905
  • 1
  • 16
  • 11
2

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.

2

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.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
1

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.

Andy Gherna
  • 2,135
  • 18
  • 22
1

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

frictionlesspulley
  • 11,070
  • 14
  • 66
  • 115
0

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

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
0

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]

David Medenjak
  • 33,993
  • 14
  • 106
  • 134
Minnie
  • 1
  • 1
-1

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

prateeksarda
  • 949
  • 1
  • 10
  • 15
-1

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.

Matt Taylor
  • 3,360
  • 1
  • 20
  • 34
Appesh
  • 374
  • 4
  • 6
-1

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.

chris
  • 9,745
  • 1
  • 27
  • 27
-1

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

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.

Munish Goyal
  • 1,379
  • 4
  • 27
  • 49
-2

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

Hengameh
  • 892
  • 7
  • 12