Difference between HashSet and HashMap?
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.
Hashtable, HashMap, HashSet , hash table concept in Java collection framework
Java's Set
and Map
interfaces specify two very different collection types. A Set
is just what it sounds like: a collection of distinct (non-equal) objects, with no other structure. A Map
is, conceptually, also just what it sounds like: a mapping from a set of objects (the distinct keys) to a collection of objects (the values). Hashtable
and HashMap
both implement Map
, HashSet
implements Set
, and they all use hash codes for keys/objects contained in the sets to improve performance.
Hashtable
and HashMap
Hashtable
is a legacy class that almost always should be avoided in favor of HashMap
. They do essentially the same thing, except most methods in Hashtable
are synchronized, making individual method calls thread-safe.1 You have to provide your own synchronization or other thread safety mechanism if you are using multiple threads and HashMap
.
The problem with Hashtable
is that synchronizing each method call (which is a not-insignificant operation) is usually the wrong thing. Either you don't need synchronization at all or, from the point of the view of the application logic, you need to synchronize over transactions that span multiple method calls. Since it was impossible to simply remove the method-level synchronization from Hashtable
without breaking existing code, the Collections framework authors needed to come up with a new class; hence HashMap
. It's also a better name, since it becomes clear that it's a kind of Map
.
Oh, if you do need method-level synchronization, you still shouldn't use Hashtable
. Instead, you can call Collections.synchronizedMap()
to turn any map into a synchronized one. Alternatively, you can use ConcurrentHashMap
, which, according to the docs: "obeys the same functional specification as Hashtable
" but has better performance and additional functionality (such as putIfAbsent()
).
1 There are other differences (less significant, in my view) such as HashMap
supporting null
values and keys.
HashSet
In terms of functionality, HashSet
has nothing to do with HashMap
. It happens to use a HashMap
internally to implement the Set
functionality. For some reason, the Collections framework developers thought it would be a good idea to make this internal implementation detail part of the public specification for the class. (This was an error, in my view.)
Java hashmap vs hashset performance
HashMap vs Vector: Inserting in HashMap is way more costlier than inserting in Vector. Although both are amortized constant time operations, but the HashMap performs a number of other operations internally (like generating hashCode, checking collissions, resolving collissions, etc), whereas the Vector just inserts the element at the end (increasing the size of the structure, if required).
HashMap vs HashSet: HashSet internally uses HashMap. So, there shouldn't be any performance difference whatsoever if you use them for the same purpose. Ideally, both of these have different purposes, so the discussion regarding which is better is useless.
Since, you need B,C,D as value for A as key, you should definitely stick to HashMap. If you really want to just compare the performance, put "null" instead of 0.0 as the value for all the keys (because that is what HashSet uses while putting the keys in its backed HashMap).
Update: HashSet uses a dummy constant value (static final) to insert in the HashMap, and not null. Sorry about that. You can replace your 0.0 with any constant and the performance should be similar to HashSet.
What's the difference between HashSet and Set?
A Set
represents a generic "set of values". A TreeSet
is a set where the elements are sorted (and thus ordered), a HashSet
is a set where the elements are not sorted or ordered.
A HashSet
is typically a lot faster than a TreeSet
.
A TreeSet
is typically implemented as a red-black tree (See http://en.wikipedia.org/wiki/Red-black_tree - I've not validated the actual implementation of sun/oracle's TreeSet
), whereas a HashSet
uses Object.hashCode()
to create an index in an array. Access time for a red-black tree is O(log(n))
whereas access time for a HashSet
ranges from constant-time to the worst case (every item has the same hashCode) where you can have a linear search time O(n)
.
Related Topics
Calling Win32 API Method from Java
Convert Arraylist into 2D Array Containing Varying Lengths of Arrays
In Java, What Are the Boolean "Order of Operations"
Java - Delete Line from Text File by Overwriting While Reading It
Converting a Jfreechart Timeseries Series with Day Data to Week or Month Data
Why Does My Spring Boot App Always Shutdown Immediately After Starting
How to Convert from Cmyk to Rgb in Java Correctly
Hash String via Sha-256 in Java
Get First Date of Current Month in Java
How to Calculate the Difference Between Two Arraylists
How to Check If a Url Exists or Returns 404 with Java
Port of Random Generator from C to Java
How to Get a Spring Bean in a Servlet Filter
Converting String to "Character" Array in Java
Why Is Executing Java Code in Comments with Certain Unicode Characters Allowed