Java Hashtable

Java Hashtable

Syntax:
java.lang.Object
       java.util.Dictionary
              java.util.Hashtable


  • Hashtable stores key/value pair in hash table.
  • This class implements a hash table, which maps keys to values.
  • It is similar to HashMap, but is synchronized.

Constructors:

  • Hashtable(): This is the default constructor.
  • Hashtable(int size): This creates a hash table that has initial size specified by size.
  • Hashtable(int size, float fillRatio): This version creates a hash table that has initial size specified by size and fill ratio specified by fillRatio. fill ratio: Basically it determines how full hash table can be before it is resized upward.and its Value lie between 0.0 to 1.0
  • Hashtable(Map m): This creates a hash table that is initialised with the elements in m.


Methods:

  • How to declare object?

Syntax: Hashtable h = new Hashtable();
Example: Hashtable hs = new Hashtable();


  • How to add entry?

Object put(Object key, Object value) :maps the specified key to the specified value in this hashtable.
Syntax :public Object put(Object key, Object value)
Example: hs.put(1, "One")


  • How to get all keys?

Enumeration keys() :used to get enumeration of the keys contained in the hash table.
Syntax :public Enumeration keys()
Returns :an enumeration of the keys in this hashtable.
Example:
Enumeration e = hs.keys();
System.out.println("display key:");
while (e.hasMoreElements()) {
System.out.println(e.nextElement());
}


  • How to check Hashtale is emptry or not?

boolean isEmpty() :used to test if this hashtable maps no keys to values.
Syntax :public boolean isEmpty()
Returns :true if this hashtable maps no keys to values; false otherwise.
Example:
if (hs.isEmpty())
System.out.println("yes hash table is empty");


  • How to view the hashcode of Hashtable?

int hashCode() :returns the hash code value for this Map as per the definition in the Map interface.
Syntax :public int hashCode()
Returns :a hash code value for this object.
Example:
System.out.println("hash code is: " + hs.hashCode());


  • How to get the value of associated key from Hashtable?

Object get(Object key) : used to get the object that contains the value associated with key.
Syntax :public Object get(Object key)
Returns :the value to which the key is mapped in this hashtable.
Example:
System.out.println(hs.get(1));


  • How to display entryset of Hashtable?

entrySet() : used to get a set view of the entries contained in this hash table.
Syntax :public Set entrySet()
Returns :returns a set view of the mappings contained in this map.
Example:
Set s = h.entrySet();
System.out.println(s);


  • How to get collection view from Hashtable?

values() :used to get a Collection view of the values contained in this Hashtable.
Syntax :public Collection values()
Returns :returns a collection view of the values contained in this map.
Example: System.out.println("collection values: " + hs.values());


  • How to get the size of Hashtable?

int size() :returns the number of entries in hash table.
Syntax :public int size()
Returns :returns the number of keys in this hashtable.
Example:
System.out.println("Size is: " + hs.size());


  • How to remove entry from Hashtable?

Object remove(Object key) :Removes key and its value.
Syntax :public Object remove(Object key)
Returns :returns the value associated with key. If key is not in the hash table, a null object is returned.
Exception :NullPointerException id specified key is null.
Example: hs.remove(1);


  • How to copies of another map into current map?

void putAll(Map t) : copies all of the mappings from the specified map to this hashtable.
Syntax :public void putAll(Map t)
Exmaple:
Hashtable h =  new Hashtable(); 
h.put(3, "THIRD");
h.put(2, "SECOND");
h.put(1, "FIRST");
hs.putAll(h);


  • How to get view of keys from Hashtable?

KeySet() :used to get a Set view of the keys contained in this hash table.
Syntax :public Set keySet()
Returns :a set view of the keys contained in this map.
Example:
Set sKey = hs.keySet();
System.out.println("key set: " + sKey);


  • How to search value in Hashtable?

boolean containsKey(Object key) : tests if some key maps into the specified value in this hashtable.
Syntax :public boolean containsKey(Object key)
Returns :returns true if and only if the specified object is a key in this hash table.
Example:
if (hs.containsKey(1))
System.out.println("Key found in table");


  • How to search value in Hashtable?

boolean containsValue(Object value) : returns true if this hash table maps one or more keys to this value.
Syntax :public boolean containsValue(Object value)
Returns :returns true if this map maps one or more keys to the specified value.
Example:
if (hs.containsValue("One"))
System.out.println("value found in table");


  • How to clear all elements?

void clear() : method clears the hashtable so that it contains no keys.
Syntax : public void clear()
Example: hs.clear()


Difference:

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form