Hashtable in Java

Hashtable in Java

Hashtable in Java

Often we need to do grouping data/ information, to manipulate or read with filters etc.  Whenever we are grouping data, how u arrange them is matters, this is what we usually call as data structuring. We already seen some data structure’s like ArrayList, LinkedList, HashSet etc . Which have different arrangements and manipulation algorithms.

This implementation stores information as a key value pair.

Whereas Hashtable follows different structuring and accessing algorithm. Before we discuss this probably you might aware of what is hash code, to put it as simple it is an integer value given to an object along with object equality relation.

Similarly might aware what is table, to keep its definition simple it is a mapping of information in columns (information metadata) with original information as well with ordering, keep this two things in mind as hashtable’s or HashMap’s algorithm to structure data and accessing these are key sense. In hashtable both key and value are any Type of objects with proper hash code and equals methods implementation In Key.

This structuring contains Array of Linked List of array’s with initial array size of 11 and a load factor value is 8.

Declaration & Initialization:

Hashtable table = new Hashtable();    à with default capacity and load factor.

Hashtable table1 = new Hashtable(22,0.90); à with capacity, load factor.

Load Factor:

It is a floating number to indicate array is overloaded and to stimulate the process of increasing array size.

Bucket:

It is one element with index of an array of linked list

Structuring algorithm:

  • Computing HashCode of Key Object by calling its hashcode method.
  • Finding index for a hashcode with AND bit wise operator of hash code and capacity-1
  • Place the Node in the appropriate bucket. If Bucket already contains Node link it as next element.

Java 8 : java 8 brought a balanced tree concept to overcome performance problem with heavy load in single bucket

Access Algorithm:

  • Computing HashCode of Key Object by calling its hashcode method.
  • Finding index for a hashcode with AND bit wise operator of hash code and capacity-1
  • Scan the Nodes in the bucket for equality of hashcodes and key by equals method then return value if match found

Access Algorithm

Methods and Constructor’s:  https://docs.oracle.com/javase/8/docs/api/java/util/Hashtable.html

Example:

Hashtable table = new Hashtable();

table.put(“srinivas”,”7878787878”);

table.put(“majella”,”99099099099”);

table.remove(“srinivas”);

System.out.println(table.size());

As Put method is named like that it indicating we do put a element in pertcular bucket. Not adding to existing group.  –  this is to remember the names of methods based on the class implementation.

HashMap:

HashMap implementation is same as Hashable except that it will allow one null key. And all methods in it are not synchronized whereas Hashtable is having synchronized methods.

Null key index always will be 0th bucket.

Methods and Constructors: https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html

Example:

HashMap map = new HashMap(); — default

map.put(“srini”,”9171443776”);

map.put(“raj”,”334776”);

map.remove(“srini”);

When to Use Hashtable:

Where there Is multi threads are modifying and simultaneously reading.

Example:

Railway tatkal ticket booking.

When to use HashMap:

Where single time write will happen and multi threads reads information from it.

Example:

Student Exam Results Report.

Leave a Reply

Your email address will not be published. Required fields are marked *

Looking for Online Training