JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Checkout for Promo Codes

Program: Basic Hashtable Operations.


Description:

Below example shows basic operations on Hashtable like creating hashtable object, adding key-value pair, getting the value based on key, checking hashtable is empty or not, removing an element, and size of the hashtable.


Code:
package com.java2novice.hashtable;

import java.util.Hashtable;

public class MyHashtableOperations {

	public static void main(String a[]){
		//Create hashtable instance
		Hashtable<String,String> ht = new Hashtable<String,String>();
		//add key-value pair to hashtable
		ht.put("first", "FIRST INSERTED");
		ht.put("second", "SECOND INSERTED");
		ht.put("third","THIRD INSERTED");
		System.out.println(ht);
		//getting value for the given key from hashtable
		System.out.println("Value of key 'second': "+ht.get("second"));
		System.out.println("Is Hashtable empty? "+ht.isEmpty());
		ht.remove("third");
		System.out.println(ht);
		System.out.println("Size of the Hashtable: "+ht.size());
	}
}

Output:
{third=THIRD INSERTED, second=SECOND INSERTED, first=FIRST INSERTED}
Value of key 'second': SECOND INSERTED
Is Hashtable empty? false
{second=SECOND INSERTED, first=FIRST INSERTED}
Size of the Hashtable: 2
Next Program >>

List Of All Hashtable Sample Programs:

  1. Basic Hashtable Operations.
  2. How to iterate through Hashtable?
  3. How to copy Map content to another Hashtable?
  4. How to search a key in Hashtable?
  5. How to search a value in Hashtable?
  6. How to get all keys from Hashtable?
  7. How to get entry set from Hashtable?
  8. How to delete all elements from Hashtable?
  9. How to read elements using Enumeration in Hashtable?
  10. Hashtable implementation with equals and hashcode example.
  11. How to fetch values with user define object as keys in Hashtable?
  12. How to eliminate duplicate keys (user defined objects) with Hashtable?
Knowledge Centre
Difference between while and do-while
The while statement verifies the condition before entering into the loop to see whether the next loop iteration should occur or not. The do-while statement executes the first iteration without checking the condition, it verifies the condition after finishing each iteration. The do-while statement will always execute the body of a loop at least once.
Famous Quotations
I respect faith, but doubt is what gets you an education.
-- Wilson Mizner

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2020 by Nataraja Gootooru. All Rights Reserved.