|
|
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
|
|
|
|
|
List Of All Hashtable Sample Programs:- Basic Hashtable Operations.
- How to iterate through Hashtable?
- How to copy Map content to another Hashtable?
- How to search a key in Hashtable?
- How to search a value in Hashtable?
- How to get all keys from Hashtable?
- How to get entry set from Hashtable?
- How to delete all elements from Hashtable?
- How to read elements using Enumeration in Hashtable?
- Hashtable implementation with equals and hashcode example.
- How to fetch values with user define object as keys in Hashtable?
- How to eliminate duplicate keys (user defined objects) with Hashtable?
|
|
|
What is fail-fast in java?
A fail-fast system is nothing but immediately report any failure that
is likely to lead to failure. When a problem occurs, a fail-fast system
fails immediately. In Java, we can find this behavior with iterators.
Incase, you have called iterator on a collection object, and another
thread tries to modify the collection object, then concurrent modification
exception will be thrown. This is called fail-fast.
The very best thing you can do for the whole world is to make the most of yourself.
-- Wallace Wattles
|