Rumah >Java >javaTutorial >Hashtable di Jawa
Di Java, Hashtable digunakan untuk menyimpan pasangan nilai kunci yang memetakan setiap kunci kepada nilai tertentu. Ia disegerakkan, tidak seperti HashMap. Selain itu, Hashtable tidak membenarkan nilai nol atau kekunci nol dan mengandungi elemen unik juga. Seperti yang telah dikatakan, jadual cincang mengandungi kunci yang dicincang dan mendapatkan kod cincang. Kemudian, kod ini akan digunakan sebagai indeks di mana nilai tertentu akan disimpan.
Mulakan Kursus Pembangunan Perisian Percuma Anda
Pembangunan web, bahasa pengaturcaraan, ujian perisian & lain-lain
Pengisytiharan Hashtable:
Kelas Hashtable boleh diisytiharkan menggunakan sintaks di bawah.
public class Hashtable<Key,Value> extends Dictionary<Key,Value> implements Map<Key,Value>, Cloneable, Serializable
Hashtable boleh dibuat menggunakan kedua-dua pembina berparameter dan tidak berparameter.
Berikut ialah kaedah yang biasa digunakan dalam HashTable.
Setiap struktur data mempunyai ciri khasnya sendiri.
Di bawah ialah contoh pelaksanaan jadual hash dalam java.
Atur cara Java untuk menambah kunci dan nilai pada jadual hash.
Kod:
//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 ")); } }
Output:
Nilai A, B, C, D dan E akan dipaparkan semasa melaksanakan kod. Selain itu, baki baharu A juga akan dipaparkan, seperti yang ditunjukkan di bawah.
Atur cara Java untuk mengalih keluar kunci dan nilai daripada jadual hash.
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); } }
Output:
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.
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")); } }
Output:
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.
Atas ialah kandungan terperinci Hashtable di Jawa. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!