The difference between HashSet HashTable HashMap

Source: Internet
Author: User

(1) HashSet is an implementation class for set, HashMap is an implementation class for map, and HashMap is a substitute for Hashtable (why is it mentioned later).

(2) HashSet takes an object as an element, and hashmap a set of objects (Key-value) as elements. And HashSet refuses to accept duplicate objects. HashMap can be viewed as three views: the set of the collection,entry of the set,value of a key. Here HashSet is actually a view of HashMap.

HashSet internal is implemented using HASHMAP, unlike HashMap, which does not require a key and value two values.

Inserting objects into the hashset is actually just an internal thing.

Public boolean Add (Object o) {

Return Map.put (o, PRESENT) ==null;
}

Now look at the difference between hasttable and HashMap:

(1) Hashtable is based on the old dictionary class, HashMap is an implementation of the map interface introduced by Java 1.2.

(2) This difference is the most important point: The method in Hashtable is synchronous, and the HashMap method (by default) is not synchronous. That is, in a multithreaded application, it is safe to use Hashtable without specialized operations, and for HashMap, an additional synchronization mechanism is required. However, the HashMap synchronization problem can be solved by a static method of collections:
Map Collections.synchronizedmap (map m)
This method returns a synchronized map that encapsulates all the methods of the underlying hashmap, making the underlying hashmap secure even in multi-threaded environments.

(3) Only HashMap can let you use NULL as the key or value of an entry for a table. Only one record in HashMap can be an empty key, but any number of entries can be empty value. This means that if a search key is not found in the table, or if a search key is found, but it is an empty value, then get () returns NULL. If necessary, use the Containkey () method to differentiate between the two cases.

Some other information:

1.HashTable method is synchronous, HashMap is not synchronized, so in multi-threaded occasions to manually synchronize HashMap this difference is like vector and ArrayList. (The main difference)
2.HashTable does not allow null values (both key and value are not allowed), HashMap allows null values (both key and value are allowed, only a key with a null value is allowed, and value can have more than one null value).
3.HashTable has a contains (object value) that functions like the Containsvalue (object value) function.
4.HashTable use Enumeration,hashmap with iterator.
The above is only the surface of the different, their implementation is very different.
The default size of the hash array in 5.HashTable is 11, and the increment is old*2+1. The default size of the hash array in HashMap is 16, and must be a 2 index.
6. Using different hash values, Hashtable directly uses the object's Hashcode, which is the code:
int hash = Key.hashcode ();
int index = (hash & 0x7FFFFFFF)% Tab.length;
Instead, HashMap recalculates the hash value and uses and replaces the modulo:
int hash = hash (k);
int i = indexfor (hash, table.length);

static int hash (Object x) {
int h = X.hashcode ();
H + = ~ (h << 9);
H ^= (H >>> 14);
H + = (h << 4);
H ^= (H >>> 10);
return h;
}
static int indexfor (int h, int length) {
Return H & (LENGTH-1);
}
These are just some of the more prominent differences, of course, their implementation is still a lot of different, such as
HashMap the operation to NULL.

The difference between HashSet HashTable HashMap

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.