• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

HashTable in Java

January 21, 2014 //  by Krishna Srinivasan//  Leave a Comment

HashTable class was originally added as part of java.utill package and extended the Dictionary class. From Java 2, it is re-engineered to implement the Map interface and made as part of the collections framework. HashTable is similar to HashMap except that HashTable is synchronized. It is very much similar to HashMap, stores the key-value pairs. HashTable supports four variations of constructors.

  • Hashtable( )
  • Hashtable(int size)
  • Hashtable(int size, float fillRatio)
  • Hashtable(Map m)

HashTable Methods

  • void clear() –> Clears all entries in this map.
  • Object clone() –> Returns a clone of the map object
  • boolean containsKey(Object key) –> Will return true if the map contains the key
  • boolean containsValue(Object value) –> Will return true if this map maps one or more keys to the specified value.
  • Enumeration elements( ) –> Returns enumeration of values
  • Object get(Object key) –> Will return the value to which the passed key is mapped in this identity hash map, or null if the map contains no mapping for the passed key.
  • boolean isEmpty() –> Will return true if this map contains no entries
  • Enumeration keys() –> Will return the set of keys in the hashtable object
  • Object put(Object key, Object value) –> Will link the key and value passed
  • void rehash( ) –>Increaes the size of the table
  • Object remove(Object key) –> Will remove the passed key-value from the map
  • int size() –> Will return the size of the map
  • String toString( ) –>Returns the string equivalent of HashTable

HashTable Example

package javabeat.net.core;

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

public class HashTableExample {

	public static void main(String args[]) {
		// Create a hash map with default constructor
		Hashtable<String,String> testHT = new Hashtable<String, String>();
		Enumeration values;
		String str;
		testHT.put("1", "Entry 1");
		testHT.put("2", "Entry 2");
		testHT.put("3", "Entry 3");

		// Show all balances in hash table.
		values = testHT.keys();
		while (values.hasMoreElements()) {
			str = (String) values.nextElement();
			System.out.println(str + ": " + testHT.get(str));
		}
	}
}

Output for the abovel example will be:

3: Entry 3
2: Entry 2
1: Entry 1

Category: JavaTag: Java Basics, Java Collections

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « HashMap in Java
Next Post: How To Sort Java Object Using Comparable And Comparator? »

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact