BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Hashtable in java with example

By Chaitanya Singh | Filed Under: java

This class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value. Hashtable is similar to HashMap except it is synchronized. There are few more differences between HashMap and Hashtable class, you can read them in detail at: Difference between HashMap and Hashtable.

In this tutorial we will see how to create a Hashtable, how to populate its entries and then we will learn how to display its key-value pairs using Enumeration. At the end of this article we will see Hashtable tutorials and methods of Hashtable class.

Example

import java.util.Hashtable;
import java.util.Enumeration;

public class HashtableExample {
 
 public static void main(String[] args) {
 
   Enumeration names;
   String key;
 
   // Creating a Hashtable
   Hashtable<String, String> hashtable = 
              new Hashtable<String, String>();
 
   // Adding Key and Value pairs to Hashtable
   hashtable.put("Key1","Chaitanya");
   hashtable.put("Key2","Ajeet");
   hashtable.put("Key3","Peter");
   hashtable.put("Key4","Ricky");
   hashtable.put("Key5","Mona");
 
   names = hashtable.keys();
   while(names.hasMoreElements()) {
      key = (String) names.nextElement();
      System.out.println("Key: " +key+ " & Value: " +
      hashtable.get(key));
   }
 }
}

Output:

Key: Key4 & Value: Ricky
Key: Key3 & Value: Peter
Key: Key2 & Value: Ajeet
Key: Key1 & Value: Chaitanya
Key: Key5 & Value: Mona

Hashtable tutorials

  • Hashtable example
  • Sort Hashtable
  • Hashtable Iterator example
  • Check key-value existence in Hashtable
  • Remove mapping from Hashtable
  • Remove all mappings from Hashtable
  • Get size of Hashtable
  • Hashtable vs HashMap

Methods of Hashtable class:

1) void clear(): Removes all the key-value mappings from Hashtable and makes it empty. Clears this hashtable so that it contains no keys..

2) Object clone(): Creates a shallow copy of this hashtable. All the structure of the hashtable itself is copied, but the keys and values are not cloned. This is a relatively expensive operation.

3) boolean contains(Object value): Tests if some key maps into the specified value in this hashtable. This operation is more expensive than the containsKey method.
Note that this method is identical in functionality to containsValue, (which is part of the Map interface in the collections framework).

4) boolean isEmpty(): Tests if this hashtable maps no keys to values.

5) Enumeration keys(): Returns an enumeration of the keys contained in the hash table.

6) Object put(Object key, Object value): Maps the specified key to the specified value in this hashtable.

7) void rehash(): Increases the size of the hash table and rehashes all of its keys.

8) Object remove(Object key): Removes the key (and its corresponding value) from this hashtable.

9) int size(): Returns the number of key-value mappings present in Hashtable.

10) String toString(): Returns the string equivalent of a hash table.

11) boolean containsKey(Object key): Tests if the specified object is a key in this hashtable.

12) boolean containsValue(Object value): Tests if the specified object is a value in this hashtable. Returns true if some value equal to value exists within the hash table. Returns false if the value isn’t found.

13) Enumeration elements(): Returns an enumeration of the values contained in the hash table.

14) Object get(Object key): Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

References:

  1. Hashtable javadoc
  2. Enumeration javadoc

Comments

  1. Raj says

    September 23, 2015 at 8:22 PM

    I tried Hashtable – it allows to put null keys as well as null values ??
    Could you please look the code

    Hashtable hashtable = new Hashtable();
    hashtable.put(“”, “ram”);
    hashtable.put(“K”, “kan”);
    hashtable.put(“S”, “”);
    System.out.println(” ” + hashtable);

    Reply
    • Binoy says

      November 13, 2015 at 5:21 AM

      FYI, “” is different than null. And you are using “” but not null. That’s why you were good to use “” as key and value. Hope it helps you.

      Reply
  2. Grijan says

    December 20, 2016 at 10:17 AM

    Why is it not printing in 1,2,3,4,5, order but in 4,3,2,1,5?

    Reply
    • Bruno says

      September 14, 2017 at 10:16 AM

      Because if you read the documentation, Hashtable is not sorted and does not guarantee the order of the keys.

      Check the link

      https://beginnersbook.com/2014/06/difference-between-hashmap-and-hashtable/

      Reply

Leave a Reply Cancel reply

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

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap