Java
Here is Java
The result of running program is:
The result of running the program that contains this fragment:
Hashtable
class is one of the oldest members of Java Collection Framework. It is an implementation of mathematical hash table data structure. In Java hashtable internally contains buckets where the key/value pairs are stored. Hashtable
is pretty similar to HashMap
. The most significant difference between them: Hashtable
is synchronized while HashMap
is not.
Hashtable as a data structure
Hashtable
is a data structure where data is stored in an array format. Every data value has a unique key value. If the key is known, access to the needed data is very fast. So, insertion and search operations are fast independently on the data size.
Hash Table consists of an array to keep data and hashing for generation an index where an element should be located.
What is hashing? It is a rule that maps the Object into a set of characters (code). Usually that kind of function converts a big piece of data into a small integer value. Hash functions could be different, but they all submit certain properties:
- A particular object has the particular hash code.
- Two equal objects have the same hash codes. The reverse is not true.
- If two hash codes are different, the objects are definitely not equal.
- Different objects may have the same hash code. This very rare event calls collision. The good hash function minimizes probability of collisions.
hashCode
.
Hashtable in Java
Hashtable
class is the implementation of a hash table data structure. This collection was created earlier than the Java Collection Framework, but was later included in it. Like all “early” collections (from Java 1.0), a hashtable is synchronized (almost all methods are marked as synchronized). Because of this factor, hashtable has significant performance problems. Hence, starting from Java 1.2, in most cases it is recommended to use other implementations of the Map
interface due to their lack of synchronization. Usually HashMap
is the most appropriate replacement.
So Class Hashtable<K,V>
consists of keys and values. It stores keys on the principle of hashing. Key-value pairs are stored in "buckets". The buckets together construct a "table", a kind of internal array. Hashtable uses the key’s hashcode to determine a bucket where the key/value pair should map.
Hash function lets to get bucket location from Key’s hashcode. This function returns an integer number for an object. As we said above two equal objects have the same hashcode, while two unequal objects might not always have different hashcodes.
Different objects, put into a hashtable may have the same hash code. To resolve this problem (collision) array of lists are used in hashtable. The pairs mapped to a single bucket are stored in a list and this list reference is stored in array index.
Hashtable Java Constructors
Hashtable()
, the default constructor. It creates an empty hashtable. (Default initial capacity = 11, load factor =0.75).Hashtable(int size)
constructs a hashtable of specified size.Hashtable(int size, float fillRatio)
creates hash table of specified size and fill ratio.- ,code>Hashtable(Map m) creates a hashtable with the same mappings as the given Map.
Hashtable Declaration
TheHashtable
Java class implements Map
, Cloneable
and Serializable
interfaces. It extends Dictionary
class.
K
is the type of keys maintained by the map.
V
is the type of mapped values.
Example:
How to import hashtable java
Java Hashtable is insidejava.util
package. So use import java.util.Hashtable;
in your code.
Usually you will get a hint from your IDE about this.
Hashtable main operations
The main operations of the Hashtable is getting, insertion to the collection and removing from there. Here these three operations are:Object get(Object key)
returns the value of the Object that has specified key. Returns null if no such key is found.Object put(Object key, Object value)
maps the specified key to the specified value. Neither the key nor the value can be null.
Object remove(Object key)
removes the entry (key and corresponding value) from hashtable.int size()
returns the quantity of entries in the hash table.boolean contains(Object value)
checks if specified value is in the hash table. If so, method returns true, else returns false.boolean containsValue(Object value)
checks if specified value is in the hash table. If so, method returns true, else returns false.void clear()
removes all entries from the hashtable.boolean containsKey(Object key)
returns true if specified key exists in the hash table, else return false.boolean isEmpty()
returns true if the hashtable is empty or false if it contains at least one key.void rehash()
increases the size of the hashtable and rehashes all of its keys.
Hash table implementation, Java code:
Let’s create aStudent
class:
Here is Java
Hashtable
example. Let’s put two Objects of the Student
class into hashtable, then remove some and check some parameters.
The result of running program is:
HashMap vs Hashtable
- Hashtable is similar to HashMap in Java. The most significant difference is that Hashtable is synchronized while HashMap is not. Therefore, Hashtable is slower than HashMap because of synchronization.
- Except of synchronization problem, Hashtable does not allow null to be used as a value or key. HashMap allows one null key and multiple null values.
- Hashtable inherits Dictionary class while HashMap inherits AbstractMap class.
- HashMap is traversed by Iterator. Hashtable can be traversed not only by Iterator but also by Enumerator.
Java hashtable example (Hashtable vs HashMap null key)
Here is a fragment code to demonstrate a null used as a key and value inHashMap
and Hashtable
The result of running the program that contains this fragment:
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.