Hashtable in Java - GeeksforGeeks
Hashtable in Java - GeeksforGeeks
Data Structures Algorithms Interview Preparation Topic-wise Practice C++ Java Python C
Imran Wahab
imran.sargodha@gmail.com
The Hashtable class implements a hash table, which maps keys to values. Any non-null
object can be used as a key or as a value. To successfully store and retrieve objects from
a hashtable, the objects used as keys must implement the hashCode method and the
equals method.
Features of Hashtable
In Hashtable we specif y an object that is used as a key, and the value we want to
associate to that key. The key is then hashed, and the resulting hash code is used as
HashMap doesn’t provide any Enumeration, while Hashtable provides not fail-fast
We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
site, Enumeration.
you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Start Your Coding Journey Now! Sign in to GeeksforGeeks
Declaration:
Login Register
with Google
public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, Clo
Imran Wahab
imran.sargodha@gmail.com
Type Parameters :
Constructors :
We
1. use cookies to ensure
Hashtable(): you have
This the best
creates an browsing
empty experience
hashtableon ourwith
website.
theBy default
using our load factor of 0.75 and
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
an initial capacity is 11.
Start Your Coding Journey Now! Sign in to GeeksforGeeks
Login
Hashtable<K, V> ht = new Hashtable<K, V>();
Register
with Google
Imran Wahab
imran.sargodha@gmail.com
Java
import java.io.*;
import java.util.*;
class AddElementsToHashtable {
public static void main(String args[])
{
// No need to mention the
// Generic type twice
Hashtable<Integer, String> ht1 = new Hashtable<>();
// Initialization of a Hashtable
// using Generics
Hashtable<Integer, String> ht2
= new Hashtable<Integer, String>();
ht2.put(4, "four");
ht2.put(5, "five");
ht2.put(6, "six");
Output
with Google
a hash table that has an initial size
Imran Wahab
Hashtable<K, V> ht = new Hashtable<K, V>(int initialCapacity);imran.sargodha@gmail.com
Java
import java.io.*;
import java.util.*;
class AddElementsToHashtable {
public static void main(String args[])
{
// No need to mention the
// Generic type twice
Hashtable<Integer, String> ht1 = new Hashtable<>(4);
// Initialization of a Hashtable
// using Generics
Hashtable<Integer, String> ht2
= new Hashtable<Integer, String>(2);
ht2.put(4, "four");
ht2.put(5, "five");
ht2.put(6, "six");
We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Start Your Coding Journey Now! Sign in to GeeksforGeeks
Output
Login Register
Mappings of ht1 : {3=three, 2=two, 1=one} with Google
college
it.gamgc@gmail.com
how full a hash table can be before it is resized upward and its Value lies between 0.0 to
1.0.
Java
import java.io.*;
import java.util.*;
class AddElementsToHashtable {
public static void main(String args[])
{
// No need to mention the
// Generic type twice
Hashtable<Integer, String> ht1
= new Hashtable<>(4, 0.75f);
// Initialization of a Hashtable
// using Generics
Hashtable<Integer, String> ht2
= new Hashtable<Integer, String>(3, 0.5f);
ht2.put(4, "four");
ht2.put(5,
We use cookies "five");
to ensure you have the best browsing experience on our website. By using our
Got It !
ht2.put(6,
site, you acknowledge that you"six");
have read and understood our Cookie Policy & Privacy Policy
Start Your Coding
// Print mappings Journey Now! : " in+ toht1);
to the console
System.out.println("Mappings of ht1 Sign
Login Register
GeeksforGeeks with Google
System.out.println("Mappings of ht2 : " + ht2);
}
} Imran Wahab
imran.sargodha@gmail.com
4. Hashtable(Map<? extends K,? extends V> m): This creates a hash table that is
Java
import java.io.*;
import java.util.*;
class AddElementsToHashtable {
public static void main(String args[])
{
// No need to mention the
// Generic type twice
Map<Integer, String> hm = new HashMap<>();
// Initialization of a Hashtable
// using Generics
We use cookies to ensure you have the best browsing experience on our website. By using our
Hashtable<Integer, String> ht2 Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
= new Hashtable<Integer, String>(hm);
Start Your Coding
// Print mappings Journey Now! Sign in to GeeksforGeeks
to the console Login Register
with Google
System.out.println("Mappings of ht2 : " + ht2);
}
Imran Wahab
}
imran.sargodha@gmail.com
Example :
Java
import java.util.*;
Output
Imran Wahab
Size of map is:- 3 imran.sargodha@gmail.com
1. Adding Elements : In order to add an element to the hashtable, we can use the put()
method. However, the inser tion order is not retained in the hashtable. Internally, for
ever y element, a separate hash is generated and the elements are indexed based on this
Java
import java.io.*;
import java.util.*;
class AddElementsToHashtable {
public static void main(String args[])
{
// No need to mention the
// Generic type twice
Hashtable<Integer, String> ht1 = new Hashtable<>();
// Initialization of a Hashtable
// using Generics
Hashtable<Integer, String> ht2
= new Hashtable<Integer, String>();
Output
2. Changing Elements : Af ter adding the elements if we wish to change the element, it
can be done by again adding the element with the put() method. Since the elements in
the hashtable are indexed using the keys, the value of the key can be changed by simply
inser ting the updated value for the key for which we wish to change.
Java
import java.io.*;
import java.util.*;
class UpdatesOnHashtable {
public static void main(String args[])
{
// Initialization of a Hashtable
Hashtable<Integer, String> ht
= new Hashtable<Integer, String>();
Output
3. Removing Element : In order to remove an element from the Map, we can use the
remove() method. This method takes the key value and removes the mapping for a key
Java
import java.io.*;
import java.util.*;
class RemovingMappingsFromHashtable {
// Initial HashMap
System.out.println("Initial map : " + ht);
We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
// Remove the map entry with key 4
ht.remove(4);
Start Your Coding Journey Now! Sign in to GeeksforGeeks
// Final Hashtable
Login Register
with Google
System.out.println("Updated map : " + ht);
}
} Imran Wahab
imran.sargodha@gmail.com
4. Traversal of a Hashtable : To iterate the table, we can make use of an advanced for
Java
import java.util.Hashtable;
import java.util.Map;
We use cookies to ensure you have the best browsing experience on our website. By using our
Output
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Start Your Coding Journey Now! Sign in to GeeksforGeeks
vaibhav 20 Login Register
with Google
vishal 10
sachin 30
Imran Wahab
imran.sargodha@gmail.com
It makes use of hashCode() method to determine which bucket the key/value pair
should map.
The hash function helps to determine the location for a given key in the bucket list.
Generally, hashcode is a non-negative integer that is equal for equal Objects and may or
may not be equal for unequal Objects. To determine whether two objects are equal or
It is possible that two unequal Objects have the same hashcode. This is called a
collision. To resolve collisions, hashtable uses an array of lists. The pairs mapped to a
single bucket (array index) are stored in a list and list reference is stored in the array
index.
Methods of Hashtable
We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Start Your Coding Journey Now! Sign in to GeeksforGeeks
K – The type of the keys in the map.
Login
V – The type of values mapped in the map.
Register
with Google
Imran Wahab
ME THOD DESCRIPTION
imran.sargodha@gmail.com
compute(K key, Attempts to compute a mapping for the specified key and
BiFunction<? super its current mapped value (or null if there is no current
mapping).
K,? super V,? extends V>
remappingFunction)
computeIfA bsent(K key, If the specified key is not already associated with a value
Function<? super K,? (or is mapped to null), attempts to compute its value using
unless null.
mappingFunction)
computeIfPresent(K key, If the value for the specified key is present and non-null,
BiFunction<? super K,? attempts to compute a new mapping given the key and its
remappingFunction)
contains(Object value) Tests if some key maps into the specified value in this
hashtable.
We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Start Your Coding Journey Now! Sign in to GeeksforGeeks
ME THOD
Login Register
with Google
DESCRIPTION
entr ySet() Returns a Set view of the mappings contained in this map.
get(Object key) Returns the value to which the specified key is mapped, or
hashCode() Returns the hash code value for this Map as per the
merge(K key, V value, If the specified key is not already associated with a value
BiFunction<? super V,? or is associated with null, associates it with the given non-
remappingFunction)
put(K key, V value) Maps the specified key to the specified value in this
hashtable.
putAll(Map<? extends K,? Copies all of the mappings from the specified map to this
We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Start Your Coding Journey Now! Sign in to GeeksforGeeks
ME THOD
Login Register
with Google
DESCRIPTION
more efficiently.
hashtable.
map.
ME THOD DESCRIPTION
forEach(BiConsumer<? Per forms the given action for each entr y in this map until all
super K,? super V> entries have been processed or the action throws an
action) exception.
key, V defaultValue) defaultValue if this map contains no mapping for the key.
putIfA bsent
(K key, V If the specified key is not already associated with a value (or
We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Start Your Coding Journey Now! Sign in to GeeksforGeeks
ME THOD
Login Register
with Google
DESCRIPTION
remove
(Object key, Removes the entr y for the specified key only if it is currently
Imran Wahab
imran.sargodha@gmail.com
mapped to the specified value.
Object value)
replace(K key, V Replaces the entr y for the specified key only if currently
replaceAll(BiFunction<? Replaces each entr y ’s value with the result of invoking the
super K,? super V,? given function on that entr y until all entries have been
Must Read:
Reference :
ml
Like 50
We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Start Your Coding Journey Now! Sign in to GeeksforGeeks
Previous Next
Login Register
with Google
Imran Wahab
imran.sargodha@gmail.com
We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Start Your Coding Journey Now! Sign in to GeeksforGeeks
Ar ticle Contributed By :
Login Register
with Google
GeeksforGeeks
Imran Wahab
imran.sargodha@gmail.com
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Load Comments
We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
Company
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Learn
About Us Algorithms
Start Your Coding Journey Now! Sign in to GeeksforGeeks
Login Register
Careers with Google Data Structures
In Media SDE Cheat Sheet
Imran Wahab
Contact Us Machine learning
imran.sargodha@gmail.com
News Languages
Python
Top News
Java
Technology
CPP
Work & Career
Golang
Business
C#
Finance
SQL
Lifestyle
Kotlin
Knowledge
We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy