The Hashtable class Of the Java collection framework implements the Hashtable, which maps the keys to the values. Any null object can be used as a key or value in this class. 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 with an initial size specified by size.
- Hashtable(int size, float fillRatio): This version creates a hash table with an initial size specified by size and
- fill ratio defined by fillRatio. fill ratio: It determines how the full hash table can be before it is resized upward. and its Value lies between 0.0 to 1.0
- Hashtable(Map m): This creates a hash table initialized with the elements in m.
How do you find an element using the 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");
}
}
}
- 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");
- }
- }
- }
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
- {third=THIRD INSERTED, second=SECOND INSERTED, first=FIRST INSERTED}
- The Hashtable contains value SECOND INSERTED
- The Hashtable does not contains value first
{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 want to share more information about the abovementioned topic.
Read Value From Hashtable Key In Java: How do you read value from a hashtable key in Java with an 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));
}
}
}
- 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));
- }
- }
- }
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
- {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
{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 comment if you find anything incorrect or want to share more information about the abovementioned topic.
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());
}
}
- 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());
- }
- }
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
- {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
{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 comment if you find anything incorrect or want to share more information about the abovementioned topic.
How do you retrieve values hashtable using keys in Java with an 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);
}
}
}
- 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);
- }
- }
- }
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
- {third=THIRD INSERTED, second=SECOND INSERTED, first=FIRST INSERTED}
- third
- second
- first
{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));
}
}
}
- 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));
- }
- }
- }
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
- Value of third is: THIRD INSERTED
- Value of second is: SECOND INSERTED
- Value of first is: FIRST INSERTED
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 that duplicates value using a 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;
}
}
}
- 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;
- }
- }
- }
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!!!
- 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!!!
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 comment if you find anything incorrect or want to share more information about the abovementioned topic.
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());
}
}
}
- 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());
- }
- }
- }
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
- {third=THIRD INSERTED, second=SECOND INSERTED, first=FIRST INSERTED}
- third ==> THIRD INSERTED
- second ==> SECOND INSERTED
- first ==> FIRST INSERTED
{third=THIRD INSERTED, second=SECOND INSERTED, first=FIRST INSERTED}
third ==> THIRD INSERTED
second ==> SECOND INSERTED
first ==> FIRST INSERTED
Please comment if you find anything incorrect or want to share more information about the abovementioned topic.