We first have a general understanding of Hashtable, and then learn its source, and finally through the example to learn to use Hashtable.
The 1th part Hashtable introduction
Part 2nd Hashtable data structure
3rd part Hashtable source analysis (based on jdk1.6.0_45)
4th Part Hashtable traversal mode
Part 5th Hashtable Example
Reprint please indicate the source: http://www.cnblogs.com/skywang12345/p/3310887.html
The 1th part hashtable introduction
Hashtable Introduction
Like HashMap, Hashtable is also a hash table that stores the key-value pair (key-value) mappings .
Hashtable inherits from Dictionary, realizes map, cloneable, java.io.Serializable interface.
Hashtable functions are synchronized , which means that it is thread-safe. Its key, value is not null. In addition, the mappings in Hashtable are not ordered.
An instance of Hashtable has two parameters that affect its performance: initial capacity and load factor . The capacity is the number of buckets in the hash table, and the initial capacity is the size of the hashtable when it was created. Note that the status of the hash table is open: When a hash conflict occurs, a single bucket stores multiple entries that must be searched sequentially. The load factor is a measure of how full a hash table can be when its capacity is automatically increased. Both the initial capacity and the load factor are just hints for the implementation. Specific details about when and whether to call the rehash method depend on the implementation.
Typically, the default load factor is 0.75, which seeks a tradeoff between time and space costs. The load factor is too high although it reduces the space overhead, it also increases the time to find an entry (which is reflected in most Hashtable operations, including get and put operations).
Constructor for Hashtable
The default constructor. Public
Hashtable ()
//Specify the constructor for capacity size public
Hashtable (int initialcapacity)
//Specify the constructor for capacity size and load factor Public
Hashtable (int initialcapacity, float loadfactor)
//constructor with "child Map" public
Hashtable (map<? Extends K,? Extends V> t)
API for Hashtable
synchronized void Clear () Synchronized Object clone () Boolean contain S (object value) synchronized Boolean ContainsKey (Object key) Synchronized Boolean containsvalue (Ob Ject value) synchronized enumeration<v> elements () synchronized set<entry<k, v>> entryset () sync Hronized Boolean equals (object) synchronized V get (object key) synchronized int Hashcode () Synchronized Boolean isempty () synchronized set<k> keyset () Synchroni Zed enumeration<k> Keys () synchronized v put (K key, v value) synchronized void Putall (map<? extends K,? extends v> Map) synchronized V Remove (Object key) synchronized int Size () synchronized String toString () synchronized collection<v> values ()
Part 2nd Hashtable data structure
The inheritance relationship of Hashtable
Java.lang.Object
↳ java.util.dictionary<k, v>
↳ java.util.hashtable<k , v>
Public Class Hashtable<k,v> extends dictionary<k,v>
implements Map<k,v>, Cloneable, java.io.Serializable {}
hashtable and map relate to the following diagram:
As you can see from the diagram:
Hashtable inherits from the dictionary class and implements the map interface. The map is the Key-value key value pair interface, dictionary is an abstract class that declares the action key value pair function interface.
Hashtable is a hash table implemented through the "zipper method". It includes several important member variables: table, Count, Threshold, Loadfactor, Modcount.
The table is a entry[] array type, and entry is actually a one-way list. The hash table's "Key-value key value pairs" are stored in the entry array.
Count is the size of the Hashtable, which is the number of key value pairs that Hashtable holds.
Threshold is a hashtable threshold used to determine if the Hashtable capacity needs to be adjusted. The value of threshold = "capacity * load factor".
Loadfactor is the load factor.
Modcount is used to implement the fail-fast mechanism.
3rd part Hashtable source analysis (based on jdk1.6.0_45)
To get a better idea of the Hashtable principle, the following is an analysis of the Hashtable source code.
In reading the source code, we recommend that the following instructions to establish a general understanding of the Hashtable, so it is easier to understand Hashtable.
1 package java.util;
2 Import java.io.*; 3 4 public class Hashtable<k,v> 5 extends dictionary<k,v> 6 implements Map<k,v> cloneabl
E, java.io.Serializable {7 8//Hashtable Save the array of Key-value.
9//Hashtable is implemented by the Zipper method, each Entry is essentially a one-way linked list of private transient entry[] table;
The actual number of elements in one//hashtable private transient int count;
14 15//threshold, used to determine whether the need to adjust hashtable capacity (threshold = capacity * load factor) private int threshold;
17 18//load factor private float loadfactor;
The number of times//Hashtable has been changed by private transient int modcount = 0;
23 24//sequence version number of private static final long serialversionuid = 1421746759512286392L; 26 27//Specifies the constructor for capacity size and load factor-public Hashtable (int initialcapacity, float loadfactor) {if (init Ialcapacity < 0 throw new IllegalArgumentException ("illegal Capacity:" + 31 Initialcapacity); if (loadfactor <= 0 | |
Float.isnan (Loadfactor)) throw new IllegalArgumentException ("Illegal Load:" +loadfactor);
if (initialcapacity==0) initialcapacity = 1;
Panax Notoginseng this.loadfactor = loadfactor;
Table = new Entry[initialcapacity];
threshold = (int) (initialcapacity * loadfactor); 40} 41 42//Specify the constructor for capacity size is public Hashtable (int initialcapacity) {(initialcapacity, 0
.75f);
45} 46 47//default constructor.
Hashtable () {49//default constructor, the specified capacity size is 11, and the load factor is 0.75 this (0.75f); 51} 52 53//The constructor containing the "child Map" is public Hashtable (map<? extends K,? extends V> t) {(
Math.max (2*t.size (), one), 0.75f);
56///Add all elements of "Hashtable" to Putall (t); Synchronized public