Let's learn java programming language with easy steps

Saturday, 24 June 2017

Java HashTable

 Java HashTable Class

In java Hashtable is a class which is available in java.util package. This class implements hash table, which maps key to values.

Java Hashtable class extends Dictionary class and implements Map interface in java collection Framework.


Some Points About Java Hashtable Class

  • Java Hashtable class implements hash table, which maps key to values.
  • Java Hashtable class extends(inherit) Dictionary class and implements Map interface.
  • Hashtable class stores the key and value pairs.
  • Hashtable class stores only unique elements.
  • Hashtable class is synchronized i.e multiple threads cannot access simultaneously. In other word Hashtable class is thread-safe.
  • Hashtable class doesn't allows null keys and null values.
  • Hashtable class is a legacy class and it uses hashing technique to store the elements i.e it doesn't maintains insertion order of an elements.
  • By using Iterator interface, for each loop and Enumeration interface you can easily retrieve the elements from the Hashtable class.


Hierarchy of Hashtable Class

Java Hashtable


Java Constructors of Hashtable Class

There are some constructors of hash table class, these are given below

1) Hashtable()

It constructs a  new, empty hashtable with the default initial capacity(11) and load factor(0.75).

2) Hashtable(int initialCapacity)

It constructs a  new, empty hashtable with the specified initial capacity and load factor(0.75).

3) Hashtable(int initialCapacity, float loadFactor)

It constructs a  new, empty hashtable with the specified initial capacity and specified load factor.

4) Hashtable(Map m)

It constructs the new hashtable with the same mapping as the given map.


Methods of Hashtable Class


There are certain methods of Hashtable class, which is

1) void clear()

This method clears this hashtable so that it contains no key.

2) Object clone()

This method creates a shallow copy of this hashtable.

3) boolean contains(Object value)

It returns true if some value equal to the value exists within the hash table, otherwise returns false.

4) boolean containsValue(Object value)

It returns true if some value equal to the value exists within the hash table, otherwise returns false.


5) boolean containsKey(Object key)

It returns true if some key equal to the key exists within the hash table, otherwise returns false.

6) boolean equals(Object o)

This method compares the specified Object with this Map for equality.

7) int hasCode()

This method returns the hash code value for this Map.

8) boolean isEmpty()

This method returns true if the hash table is empty otherwise it returns false.

9) void putAll(Map t)

This methods copies all of the mappings from the specified map to this hash table.

10) protected void rehash()

It is use to increase the size of the hash table.

11) int size()

This method returns the number of the keys in this hashtable.


Java Hashtable Example

This is simple example of hash table class in java collection.

import java.util.*;
class HashTableExample
{
public static void main(String args[])
{
//creating Hashtable object
Hashtable<Integer, String> ht = new Hashtable<Integer, String>();
ht.put(7, "sachin");
ht.put(9, "virat");
ht.put(5, "rohit");
ht.put(2, "bumrah");
ht.put(8, "dhoni");

for(Map.Entry me : ht.entrySet())
{
System.out.println(me.getKey()+" "+me.getValue());
}
}
}

output : 9 virat
              8 dhoni
              7 sachin
              5 rohit
              2 bumrah 
Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on Google+

Follow javatutorial95 on twitter

Popular Posts

Get Updates By Email

Link

Translate