Apart from the fact that hashSet does not allow duplicate values, what is the difference between a HashMap and Hashset...?
I mean implementaion wise.....? It's a little bit vague because both use hash table to store values.....
Apart from the fact that hashSet does not allow duplicate values, what is the difference between a HashMap and Hashset...?
I mean implementaion wise.....? It's a little bit vague because both use hash table to store values.....
A HashSet is implemented in terms of a HashMap. It's a mapping between the key and a PRESENT object.
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.
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.
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.
As the names imply, a HashMap is an associative Map (mapping from a key to a value), a HashSet is just a Set.
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, the Tutorial is a good starting place. If you truly want to know what's going on, there's a book for that, too.
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.
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.
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).
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.