views:

872

answers:

10

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

+2  A: 

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

Matthew Flaschen
+1  A: 

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.

oedo
@oedo - `java.util.HashSet` says that is is backed by a `java.util.HashMap`.
justkt
+1  A: 

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
That's not right. The set elements are directly used as HashMap keys.
Matthew Flaschen
+1  A: 

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
+2  A: 

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

leonbloy
But in hashSet a hash code maps to a value....
SpikETidE
@SpikETidE That's a detail of how uniqueness is implemented, but the meaning of HashSet is to implement a set.
Michael Borgwardt
so.. it all boils down to "if you don't want duplicates use hashSet... If you don't bother about duplicates use HashMap"....?
SpikETidE
@SpikETidE: no, that's completely wrong.
Michael Borgwardt
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
+15  A: 

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.

justkt
The key look up occurs using the hash.....So, there may be a hash code mapped to more than one key...?
SpikETidE
so.. it all boils down to "if you don't want duplicates use hashSet... If you don't bother about duplicates use HashMap"....?
SpikETidE
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 tothe object's own hashCode, this method applies a "supplementalhash function," which defends against _poor_ quality hash functions.This is critical because HashMap uses power-of two lengthhash 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
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
@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
@SpikETidE: neither HashMap nor HashSet allow duplicates. That's the whole point.
Michael Borgwardt
Oh yes.... set in values and map in keys....
SpikETidE
@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
@SpikETidE - if you haven't already, I'd definitely suggest reading the Collections tutorial or the excellent book Java Generics and Collections to get an overview of the Collections constructs.
justkt
@Michael Borgwardt - thanks for your clarifications!
justkt
+10  A: 

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.

Bruno Rothgiesser
+1, sums it up very well.
Zaki
+1 clear and concise answer
Yatendra Goel
A: 

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
+1  A: 

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
+1  A: 

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