Java Collections

Hashtable class in collection framework

  • A Hashtable is an array of list.Each list is known as a bucket.The position of bucket is identified by calling the hashcode() method. A Hashtable contains values based on the key. It implements the Map interface and extends Dictionary class.
  • It contains only unique elements.
  • It may have not have any null key or value.
  • It is synchronized.

Like HashMap, Hashtable stores key/value pairs in a hash table. When using a Hashtable, you specify an object that is used as a key, and the value that you want linked to that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored within the table.

Constructors:

The Hashtable defines four constructors. The first version is the default constructor:

Hashtable( )

The second version creates a hash table that has an initial size specified by size:

Hashtable(int size)

The third version creates a hash table that has an initial size specified by size and a fill ratio specified by fillRatio.

This ratio must be between 0.0 and 1.0, and it determines how full the hash table can be before it is resized upward.

Hashtable(int size, float fillRatio)

The fourth version creates a hash table that is initialized with the elements in m.

The capacity of the hash table is set to twice the number of elements in m. The default load factor of 0.75 is used.

Hashtable(Map m)

Methods:
Apart from the methods defined by Map interface, Hashtable defines following methods:

SN Methods with Description
1 void clear( )
Resets and empties the hash table.
2 Object clone( )
Returns a duplicate of the invoking object.
3 boolean contains(Object value)
Returns true if some value equal to value exists within the hash table. Returns false if the value isn’t found.
4 boolean containsKey(Object key)
Returns true if some key equal to key exists within the hash table. Returns false if the key isn’t found.
5 boolean containsValue(Object value)
Returns true if some value equal to value exists within the hash table. Returns false if the value isn’t found.
6 Enumeration elements( )
Returns an enumeration of the values contained in the hash table.
7 Object get(Object key)
Returns the object that contains the value associated with key. If key is not in the hash table, a null object is returned.
8 boolean isEmpty( )
Returns true if the hash table is empty; returns false if it contains at least one key.
9 Enumeration keys( )
Returns an enumeration of the keys contained in the hash table.
10 Object put(Object key, Object value)
Inserts a key and a value into the hash table. Returns null if key isn’t already in the hash table; returns the previous value associated with key if key is already in the hash table.
11 void rehash( )
Increases the size of the hash table and rehashes all of its keys.
12 Object remove(Object key)
Removes key and its value. Returns the value associated with key. If key is not in the hash table, a null object is returned.
13 int size( )
Returns the number of entries in the hash table.
14 String toString( )
Returns the string equivalent of a hash table.

Example of Hashtable:

import java.util.*;
class Simple{
 public static void main(String args[]){
 
  Hashtable hm = new Hashtable();

  hm.put(100,"Dinesh");
  hm.put(102,"Sweety");
  hm.put(101,"Ankit");
  hm.put(103,"Nimmo");

  Set set = hm.entrySet();
  Iterator itr = set.iterator();

  while(itr.hasNext()){
   Map.Entry m=(Map.Entry)itr.next();
   System.out.println(m.getKey()+" "+m.getValue());
  }
 }
}
Output:
103 Rahul
102 Ravi
101 Vijay
100 Amit
<<Previous <<   || Index ||   >>Next >>
Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago