- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Hashtable in Java
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.
Hashtable defines the following methods −
Sr.No | Method & 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 the 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 the 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 the 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 the key. If the 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 the key isn't already in the hash table; returns the previous value associated with the key if the key is already in the hash table. |
Example
Let us see an example to implement Hashtable −
import java.util.*; public class Demo { public static void main(String args[]) { Hashtable balance = new Hashtable(); Enumeration names; String str; double bal; balance.put("Jacob", new Double(3434.34)); balance.put("Ryan", new Double(123.22)); balance.put("Nathan", new Double(1378.00)); balance.put("Ken", new Double(99.22)); balance.put("Kevin", new Double(-19.08)); // Show all balances in hash table. names = balance.keys(); while(names.hasMoreElements()) { str = (String) names.nextElement(); System.out.println(str + ": " + balance.get(str)); } System.out.println(); // Deposit 1,000 into Nathan's account bal = ((Double)balance.get("Nathan")).doubleValue(); balance.put("Nathan", new Double(bal + 1000)); System.out.println("Nathan's new balance: " + balance.get("Nathan")); } }
Output
Ken: 99.22 Nathan: 1378.0 Jacob: 3434.34 Ryan: 123.22 Kevin: -19.08 Nathan's new balance: 2378.0

Advertisements