• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Java Program
  • Selenium
  • Selenium Programs
  • Manual Testing
  • Difference
  • Tools
  • SQL
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Tutorial » Hashtable Class In Java

Hashtable Class In Java

Last Updated on: August 15, 2020 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Key Points about Hashtable Class
  • Hashtable ClassConstructors

Hashtable class Of Java collection framework implements the Hashtable, which maps the keys to the values. In this class, any null object can be used as a key or as a value. If we compare the Hashtable with the HashMap, you will find the Hashtable is synchronized, whereas HashMap is not synchronized.

Key Points about Hashtable Class

  • It Implements Hashtable.
  • It is synchronized.
  • It does not accept the null key or values.
  • It does not allow duplicate keys.

Hashtable ClassConstructors

  • Hashtable(): This is the default constructor.
  • Hashtable(int size): This creates a hash table that has an initial size specified by size.
  • Hashtable(int size, float fillRatio): This version creates a hash table that has an initial size specified by size and
  • fills ratio defined by fillRatio. fill ratio: Basically, it determines how the 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 initialized with the elements in m.

How to Find Element Using containsValue() From Hashtable In Java?

package com.java.Softwaretestingblog;
import java.util.Hashtable;
public class HashtableValueSearch {
   public static void main(String[] args) {
   // TODO Auto-generated method stub
   //www.softwaretestingblog.in
   Hashtable<String, String> hm = new Hashtable<String, String>();
        //add key-value pair to Hashtable
        hm.put("first", "FIRST INSERTED");
        hm.put("second", "SECOND INSERTED");
        hm.put("third","THIRD INSERTED");
        System.out.println(hm);
        if(hm.containsValue("SECOND INSERTED"))
        {
            System.out.println("The Hashtable contains value SECOND INSERTED");
        }
        else
        {
            System.out.println("The Hashtable does not contains value SECOND INSERTED");
        }
        if(hm.containsValue("first"))
        {
            System.out.println("The Hashtable contains value first");
        }
        else
        {
            System.out.println("The Hashtable does not contains value first");
        }
   }
}

Output:

{third=THIRD INSERTED, second=SECOND INSERTED, first=FIRST INSERTED}
The Hashtable contains value SECOND INSERTED
The Hashtable does not contains value first

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Read Value From Hashtable Key In Java: How to Read Value From Hashtable Key In Java With Example?

package com.java.Softwaretestingblog;
import java.util.Hashtable;
import java.util.Set;
public class HashtableKey_Example {
   public static void main(String[] args) {
   // TODO Auto-generated method stub
   //www.softwaretestingblog.in
   Hashtable<String, String> hm = new Hashtable<String, String>();
   //add key-value pair to Hashtable
   hm.put("first", "FIRST INSERTED");
   hm.put("second", "SECOND INSERTED");
   hm.put("third","THIRD INSERTED");
   System.out.println(hm);
   Set<String> keys = hm.keySet();
   for(String key: keys){
      System.out.println("Value of "+key+" is: "+hm.get(key));
   }
   }
}

Output:

{third=THIRD INSERTED, second=SECOND INSERTED, first=FIRST INSERTED}
Value of third is: THIRD INSERTED
Value of second is: SECOND INSERTED
Value of first is: FIRST INSERTED

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Hashtable Operation in Java:  Different Hashtable Operation In Java With Example?

package com.softwaretestingo.java.basics;
import java.util.Hashtable;
public class HashtableOperations 
{
   public static void main(String[] args) 
   {
      //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

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

How to Retrieve Values Hashtable Using Keys in Java With Example?

package com.java.Softwaretestingblog;
import java.util.Hashtable;
import java.util.Set;
public class Retrieve_Values_Hashtable_Keys {
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      //www.softwaretestingblog.in
      Hashtable<String, String> hm = new Hashtable<String, String>();
      //add key-value pair to Hashtable
      hm.put("first", "FIRST INSERTED");
      hm.put("second", "SECOND INSERTED");
      hm.put("third","THIRD INSERTED");
      System.out.println(hm);
      Set<String> keys = hm.keySet();
      for(String key: keys){
         System.out.println(key);
      }
   }
}

Output:

{third=THIRD INSERTED, second=SECOND INSERTED, first=FIRST INSERTED}
third
second
first

Retain Elements HashTable In Java Collection Program: Write a Program to Retain Elements HashTable in Java?

package com.java.Softwaretestingblog;
import java.util.Enumeration;
import java.util.Hashtable;
public class Retain_Elements_HashTable {
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      //www.softwaretestingblog.in
      Hashtable<String, String> hm = new Hashtable<String, String>();
        //add key-value pair to Hashtable
        hm.put("first", "FIRST INSERTED");
        hm.put("second", "SECOND INSERTED");
        hm.put("third","THIRD INSERTED");
        Enumeration<String> keys = hm.keys();
        while(keys.hasMoreElements())
        {
            String key = keys.nextElement();
            System.out.println("Value of "+key+" is: "+hm.get(key));
        }
   }
}

Output:

Value of third is: THIRD INSERTED
Value of second is: SECOND INSERTED
Value of first is: FIRST INSERTED

Duplicate Value Using HashTable In Java: Write a Program Duplicate Value Using HashTable In Java?

package com.java.Softwaretestingblog;
import java.util.Hashtable;
import java.util.Set;
public class Duplicate_Value_HashSet {
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      //www.softwaretestingblog.in
      Hashtable<Empl,String> tm = new Hashtable<Empl, String>();
        tm.put(new Empl(134,"Ram",3000), "RAM");
        tm.put(new Empl(235,"John",6000), "JOHN");
        tm.put(new Empl(876,"Crish",2000), "CRISH");
        tm.put(new Empl(512,"Tom",2400), "TOM");
        System.out.println("Adding duplicate entry:");
        tm.put(new Empl(512,"Tom",2400), "TOM");
        System.out.println("Hashtable entries:");
        Set<Empl> keys = tm.keySet();
        for(Empl key:keys){
            System.out.println(key+" ==> "+tm.get(key));
        }
        System.out.println("Duplicate got eliminated!!!");
   }
}
class Empl{
    private String name;
    private int salary;
    private int id;
    public Empl(int id, String n, int s){
        this.id = id;
        this.name = n;
        this.salary = s;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }
    public String toString(){
        return "Id: "+this.id+" -- Name: "+this.name+" -- Salary: "+this.salary;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getId() {
        return id;
    }
    @Override
    public int hashCode() {
        System.out.println("In hashcode");
        return this.getId();
    }
    @Override
    public boolean equals(Object obj) {
        Empl e = null;
        if(obj instanceof Empl){
            e = (Empl) obj;
        }
        System.out.println("In equals");
        if(this.getId() == e.getId()){
            return true;
        } else {
            return false;
        }        
    }
}

Output:

In hashcode
In hashcode
In hashcode
In hashcode
Adding duplicate entry:
In hashcode
In equals
Hashtable entries:
In hashcode
In equals
Id: 876 -- Name: Crish -- Salary: 2000 ==> CRISH
In hashcode
In equals
Id: 512 -- Name: Tom -- Salary: 2400 ==> TOM
In hashcode
In equals
Id: 235 -- Name: John -- Salary: 6000 ==> JOHN
In hashcode
In equals
Id: 134 -- Name: Ram -- Salary: 3000 ==> RAM
Duplicate got eliminated!!!

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Add Values HashTable In Java: How to Add Values HashTable In Java With Example?

package com.java.Softwaretestingblog;
import java.util.Hashtable;
import java.util.Map.Entry;
import java.util.Set;
public class Add_Values_HashTable {
   public static void main(String[] args) {
   // TODO Auto-generated method stub
   //www.softwaretestingblog.in
   Hashtable<String, String> hm = new Hashtable<String, String>();
        //add key-value pair to Hashtable
        hm.put("first", "FIRST INSERTED");
        hm.put("second", "SECOND INSERTED");
        hm.put("third","THIRD INSERTED");
        System.out.println(hm);
        //getting value for the given key from Hashtable
         Set<Entry<String, String>> entires = hm.entrySet();
        for(Entry<String,String> ent:entires){
            System.out.println(ent.getKey()+" ==> "+ent.getValue());
        }
   }
}

Output:

{third=THIRD INSERTED, second=SECOND INSERTED, first=FIRST INSERTED}
third ==> THIRD INSERTED
second ==> SECOND INSERTED
first ==> FIRST INSERTED

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Reference: Article

    Filed Under: Java Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers