Home > Java > javaTutorial > body text

Hashtable in Java

WBOY
Release: 2024-08-30 15:47:42
Original
1007 people have browsed it

In Java, Hashtable is used to store key-value pairs that map each and every key to certain values. It is synchronized, unlike the HashMap. Moreover, Hashtable does not allow null values or null keys and contains unique elements as well. As already said, the hash table contains a key that is hashed and obtain a hash code. Then, this code will be used as an index where the particular value will be stored.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Declaration of Hashtable:

Hashtable class can be declared using the below syntax.

public class Hashtable<Key,Value> extends Dictionary<Key,Value> implements Map<Key,Value>, Cloneable, Serializable
Copy after login

Constructors of Hashtable

Hashtable can be created using both parameterized and non-parameterized constructors.

  • Hashtable(): A new, as well as an empty hashtable, will be constructed with a load factor of 0.75 and an initial capacity of 11. Here the initial capacity and load factor are the default.
  • Hashtable (int initCapacity): A new, as well as an empty hashtable, will be constructed with mentioned initial capacity and load factor as 0.75. Here, only the load factor is the default.
  • Hashtable (int initCapacity, float loadfact): A new, as well as an empty hashtable, will be constructed with the mentioned initial capacity and load factor.
  • Hashtable (Map t): A new hashtable will be constructed with mappings the same as that of the mentioned map.

Methods of Hashtable

The following are the commonly used methods in HashTable.

  • clear(): This hashtable will be cleared with no keys remaining.
  • contains(Object val ): This method tests whether any key is mapping into the value val in the hashtable.
  • clone(): A shallow copy will be created for the hashtable.
  • containsKey(Object k ): This method tests whether the specified key k is available in the hashtable.
  • contains a value(Object val ): If the hashtable maps 1 or more than 1 key to the mentioned value val, true will be returned.
  • elements(): Value’s enumeration will be returned.
  • keys(): Key’s enumeration will be returned.
  • entrySet(): A set view will be returned for the mappings present on the map.
  • equals(Object o): A map will be compared with the specified object.
  • get(Objectkey): If the specified key is mapped into any value, it will be returned. If nothing is mapped, null will be returned.
  • isEmpty(): This method checks whether the hashtable is not mapping keys to any values.
  • keySet(): A set view will be returned for the keys present in the map.
  • size(): The count of keys in the hashtable will be returned.
  • hashCode(): Hash code value will be returned for the map.
  • put(key, Val value): Key k will be mapped to the value val in the hashtable.
  • putAll(MapKey,? extends Val> t): The mappings will be copied from the mentioned map to the hashtable.
  • remove(Object k): Key k and the corresponding value will be removed from the table.
  • values(): A collection view will be returned for the values present in the map.

Examples to Implement Hashtable in Java

Each data structure has its own special features.

Below are the examples of implementing hashtable in java.

Example #1

Java program to add keys and values to the hashtable.

Code:

//Java program to add keys and values to the hashtable
import java.util.Enumeration;
import java.util.Hashtable;
//class
public class HashTableExample {
//main method
public static void main(String args[]) {
// Hashtable creation
Hashtable htbl = new Hashtable();
//create an enumeration enm
Enumeration enm;
//create sing s
String s;
//create a double variable balance
double balance;
//add keys and values to the table
htbl.put(" A ", new Double(3500.50));
htbl.put(" B ", new Double(2900.00));
htbl.put(" C ", new Double(3600.00));
htbl.put(" D ", new Double(4550.50));
htbl.put(" E ", new Double(2345.67));
// Store all the keys in the enumeration enm
enm = htbl.keys();
//if more elements are present in the enm, enter this loop
while(enm.hasMoreElements()) {
s = (String) enm.nextElement();
System.out.println(s + ": " + htbl.get(s));
}
System.out.println();
// Add 1000 to value of Key A
balance = ((Double)htbl.get(" A ")).doubleValue();
htbl.put(" A ", new Double(balance + 1000));
System.out.println(" A's new balance : " + htbl.get(" A "));
}
}
Copy after login

Output:

Values of A, B, C, D, and E will be displayed on executing the code. Moreover, the new balance of A will also be displayed, as shown below.

Hashtable in Java

Example #2

Java program to remove keys and values from the hashtable.

Code:

//Java program to remove keys and values from the hashtable
import java.util.Enumeration;
import java.util.Hashtable;
//class
public class HashTableExample {
//main method
public static void main(String args[]) {
// Hashtable creation
Hashtable<Integer,String> htbl = new Hashtable<Integer,String>();
//add keys and values to the table
htbl.put(1,"29");
htbl.put(2,"30");
htbl.put(3,"31");
htbl.put(4,"32");
htbl.put(5,"33");
htbl.put(6,"34");
htbl.put(7,"35");
System.out.println("Hashtable before removing values: "+ htbl);
// Remove 6 and 3
htbl.remove(6);
htbl.remove(3);
System.out.println("Hashtable after removing values : "+ htbl);
}
}
Copy after login

Output:

Hashtable in Java

In this program, values of 1, 2, 3, 4, 5, 6 and 7 will be displayed on executing the code. Then the values of 6 and 3 will be removed and display the rest of the values.

Example #3

Java program to get keys and values from the hashtable.

Code:

//Java program to get keys and values from the hashtable
import java.util.Enumeration;
import java.util.Hashtable;
//class
public class HashTableExample {
//main method
public static void main(String args[]) {
// Hashtable creation
Hashtable<Integer,String> htbl = new Hashtable<Integer,String>();
//add keys and values to the table
htbl.put(1,"29");
htbl.put(2,"30");
htbl.put(3,"31");
htbl.put(4,"32");
htbl.put(5,"33");
htbl.put(6,"34");
htbl.put(7,"35");
System.out.println("Hashtable : "+ htbl);
//if value of 3 is present, then return it else print Null
System.out.println(htbl.getOrDefault(3, "Null"));
//if value of 8 is present, then return it else print Null
System.out.println(htbl.getOrDefault(8, "Null"));
}
}
Copy after login

Output:

Hashtable in Java

In this program also, values of 1, 2, 3, 4, 5, 6 and 7 will be displayed on executing the code. Then, the values for keys 3 and 8 will be retrieved using the method getOrDefault(). Since the value of 8 is not available, null will be returned in the second getOrDefault() method.

The above is the detailed content of Hashtable in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!