the difference between HashMap and Hashtable, the relationship between HashMap and HashSetBlog Category:Core Java turn from http://blog.csdn.net/wl_ldy/article/details/5941770
Hashtable is widely used, HashMap is the class used in the new framework to replace Hashtable, that is to say HashMap is recommended, do not use Hashtable. Maybe you think Hashtable is very useful, why not. Here is a simple analysis of their differences.
One: The difference between HashMap and Hashtable
1.HashTable method is synchronous, in front of the method has synchronized to synchronize, HashMap is not synchronized, so in multi-threaded occasions to manually synchronize
HashMap the difference is like vectors and ArrayList.
1. You can use
2. Map m = collections.synchronizedmap (new HashMap (...));
3. Synchronize the HashMap.
Can be via Map m = Collections.synchronizedmap (new HashMap (...)); Synchronize the HashMap.
2.HashTable does not allow null values (neither key nor value), HashMap allows null values (both key and value).
3.HashTable has a contains (object value) feature and Containsvalue (object value) functionality.
4.HashTable uses enumeration for traversal, HashMap uses iterator for traversal.
HashMap Traversal Reference: http://blog.csdn.net/wl_ldy/archive/2010/10/13/5939552.aspx
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) {
H ^= (H >>>) ^ (h >>> 12);
Return h ^ (H >>> 7) ^ (H >>> 4);
}
static int indexfor (int h, int length) {
Return H & (LENGTH-1);
}
These are just some of the more prominent differences.
Ii. the relationship between HashMap and HashSet
1, HashSet Bottom is implemented using HashMap:
1. Public HashSet () {
2. Map = new hashmap<e,object> ();
3.}
Public HashSet () {map = new hashmap<e,object> ();}
2.
When you call HashSet's Add method, you actually add a line to the HashMap (Key-value pair), and the key of the row is the object that is added to the HashSet, and the value of the row is a constant of type object.
1. Private static Final Object PRESENT = new Object ();
2. Public boolean Add (E e) {
3. Return Map.put (E, PRESENT) ==null;
4.}
5. Public boolean remove (Object o) {
6. Return Map.Remove (o) ==present;
7.}
Private static Final Object PRESENT = new Object (); Public boolean Add (E-e) {return Map.put (E, PRESENT) ==null;} public boolean remove (Object o) {return map.remove (o) ==pres ENT; }
Third, the properties of the characteristics of the class, whether thread-safe.
Java.util.Properties inherits from Java.util.Hashtable, so the Properties are thread-safe.
The Properties class represents a persistent property set. Properties can be saved in a stream or loaded from a stream. Each key and its corresponding value in the attribute list is a string.
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.