In hashing there is a hash function that maps keys to some values. But these hashing function may lead to collision that is two or more keys are mapped to same value. Chain hashing avoids collision. The idea is to make each cell of hash table point to a linked list of records that have same hash function value.
Let’s create a hash function, such that our hash table has ‘N’ number of buckets.
To insert a node into the hash table, we need to find the hash index for the given key. And it could be calculated using the hash function.
Example: hashIndex = key % noOfBuckets
Insert: Move to the bucket corresponds to the above calculated hash index and insert the new node at the end of the list.
Delete: To delete a node from hash table, calculate the hash index for the key, move to the bucket corresponds to the calculated hash index, search the list in the current bucket to find and remove the node with the given key (if found).

Please refer Hashing | Set 2 (Separate Chaining) for details.
Methods to implement Hashing in Java
- With help of HashTable (A synchronized implementation of hashing)
Java
import java.util.*;
class GFG {
public static void main(String args[])
{
Hashtable<Integer, String>
hm = new Hashtable<Integer, String>();
hm.put( 1 , "Geeks" );
hm.put( 12 , "forGeeks" );
hm.put( 15 , "A computer" );
hm.put( 3 , "Portal" );
System.out.println(hm);
}
}
|
Output
{15=A computer, 3=Portal, 12=forGeeks, 1=Geeks}
- With the help of HashMap (A non-synchronized faster implementation of hashing)
Java
import java.util.*;
class GFG {
static void createHashMap( int arr[])
{
HashMap<Integer, Integer> hmap = new HashMap<Integer, Integer>();
for ( int i = 0 ; i < arr.length; i++) {
Integer c = hmap.get(arr[i]);
if (hmap.get(arr[i]) == null ) {
hmap.put(arr[i], 1 );
}
else {
hmap.put(arr[i], ++c);
}
}
System.out.println(hmap);
}
public static void main(String[] args)
{
int arr[] = { 10 , 34 , 5 , 10 , 3 , 5 , 10 };
createHashMap(arr);
}
}
|
Output
{34=1, 3=1, 5=2, 10=3}
- With the help of LinkedHashMap (Similar to HashMap, but keeps order of elements)
Java
import java.util.*;
public class BasicLinkedHashMap
{
public static void main(String a[])
{
LinkedHashMap<String, String> lhm =
new LinkedHashMap<String, String>();
lhm.put( "one" , "practice.geeksforgeeks.org" );
lhm.put( "two" , "code.geeksforgeeks.org" );
lhm.put( "four" , "www.geeksforgeeks.org" );
System.out.println(lhm);
System.out.println( "Getting value for key 'one': "
+ lhm.get( "one" ));
System.out.println( "Size of the map: " + lhm.size());
System.out.println( "Is map empty? " + lhm.isEmpty());
System.out.println( "Contains key 'two'? " +
lhm.containsKey( "two" ));
System.out.println( "Contains value 'practice.geeks"
+ "forgeeks.org'? " + lhm.containsValue( "practice" +
".geeksforgeeks.org" ));
System.out.println( "delete element 'one': " +
lhm.remove( "one" ));
System.out.println(lhm);
}
}
|
Output
{one=practice.geeksforgeeks.org, two=code.geeksforgeeks.org, four=www.geeksforgeeks.org}
Getting value for key 'one': practice.geeksforgeeks.org
Size of the map: 3
Is map empty? false
Contains key 'two'? true
Contains value 'practice.geeksforgeeks.org'? true
delete element 'one': practice.geeksforgeeks.org
{two=code.geeksforgeeks.org, four=www.geeksforgeeks.org}
- With the help of ConcurretHashMap(Similar to Hashtable, Synchronized, but faster as multiple locks are used)
Java
import java.util.concurrent.*;
class ConcurrentHashMapDemo {
public static void main(String[] args)
{
ConcurrentHashMap<Integer, String> m =
new ConcurrentHashMap<Integer, String>();
m.put( 100 , "Hello" );
m.put( 101 , "Geeks" );
m.put( 102 , "Geeks" );
System.out.println( "ConcurrentHashMap: " + m);
m.putIfAbsent( 101 , "Hello" );
System.out.println( "\nConcurrentHashMap: " + m);
m.remove( 101 , "Geeks" );
System.out.println( "\nConcurrentHashMap: " + m);
m.replace( 100 , "Hello" , "For" );
System.out.println( "\nConcurrentHashMap: " + m);
}
}
|
Output
ConcurentHashMap: {100=Hello, 101=Geeks, 102=Geeks}
ConcurentHashMap: {100=Hello, 101=Geeks, 102=Geeks}
ConcurentHashMap: {100=Hello, 102=Geeks}
ConcurentHashMap: {100=For, 102=Geeks}
- With the help of HashSet (Similar to HashMap, but maintains only keys, not pair)
Java
import java.util.*;
class Test {
public static void main(String[] args)
{
HashSet<String> h = new HashSet<String>();
h.add( "India" );
h.add( "Australia" );
h.add( "South Africa" );
h.add( "India" );
System.out.println(h);
System.out.println( "\nHashSet contains India or not:"
+ h.contains( "India" ));
h.remove( "Australia" );
System.out.println( "\nList after removing Australia:" + h);
System.out.println( "\nIterating over list:" );
Iterator<String> i = h.iterator();
while (i.hasNext())
System.out.println(i.next());
}
}
|
Output
[South Africa, Australia, India]
HashSet contains India or not:true
List after removing Australia:[South Africa, India]
Iterating over list:
South Africa
India
With the help of LinkedHashSet (Similar to LinkedHashMap, but maintains only keys, not pair)
Java
import java.util.LinkedHashSet;
public class Demo
{
public static void main(String[] args)
{
LinkedHashSet<String> linkedset =
new LinkedHashSet<String>();
linkedset.add( "A" );
linkedset.add( "B" );
linkedset.add( "C" );
linkedset.add( "D" );
linkedset.add( "A" );
linkedset.add( "E" );
System.out.println( "Size of LinkedHashSet = " +
linkedset.size());
System.out.println( "Original LinkedHashSet:" + linkedset);
System.out.println( "Removing D from LinkedHashSet: " +
linkedset.remove( "D" ));
System.out.println( "Trying to Remove Z which is not " +
"present: " + linkedset.remove( "Z" ));
System.out.println( "Checking if A is present=" +
linkedset.contains( "A" ));
System.out.println( "Updated LinkedHashSet: " + linkedset);
}
}
|
Output
Size of LinkedHashSet = 5
Original LinkedHashSet:[A, B, C, D, E]
Removing D from LinkedHashSet: true
Trying to Remove Z which is not present: false
Checking if A is present=true
Updated LinkedHashSet: [A, B, C, E]
- With the help of TreeSet (Implements the SortedSet interface, Objects are stored in a sorted and ascending order).
Java
import java.util.*;
class TreeSetDemo {
public static void main(String[] args)
{
TreeSet<String> ts1 = new TreeSet<String>();
ts1.add( "A" );
ts1.add( "B" );
ts1.add( "C" );
ts1.add( "C" );
System.out.println( "TreeSet: " + ts1);
System.out.println( "\nTreeSet contains A or not:"
+ ts1.contains( "A" ));
ts1.remove( "A" );
System.out.println( "\nTreeSet after removing A:" + ts1);
System.out.println( "\nIterating over TreeSet:" );
Iterator<String> i = ts1.iterator();
while (i.hasNext())
System.out.println(i.next());
}
}
|
Output
TreeSet: [A, B, C]
TreeSet contains A or not:true
TreeSet after removing A:[B, C]
Iterating over TreeSet:
B
C
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
01 Mar, 2023
Like Article
Save Article