You are on page 1of 4

HASTABLE TRONG JAVA

1. HashTable trong Java là gì?


Lớp Java HashTable cài đặt (implement) một bảng HashTable để map khóa và giá trị.
HashTable kế thừa lớp Dictionary và cài đặt (implement) Map Interface.

Các đặc điểm quan trọng về lớp HashTable trong java là:
• HashTable là một mảng của list. Mỗi list được biết đến như một bucket (vùng
chứa) các phần tử. Vị trí của một bucket được xác định bằng việc gọi phương thức
hashcode().
• HashTable cũng lưu trữ dữ liệu dưới dạng cặp key và value.
• HashTable chứa các key duy nhất.
• HashTable KHÔNG thể có bất kỳ key hoặc giá trị nào là null.
• HashTable được đồng bộ (synchronized).
1.1 Khởi tạo Hashtable
Để khởi tạo một Hashtable trong Java, chúng ta cần import gói java.util và tạo một đối
tượng của lớp Hashtable.
import java.util.Hashtable;

public class Main {


public static void main(String[] args) {
// Khởi tạo Hashtable với kiểu dữ liệu String cho cả key và value
Hashtable<String, Integer> hashtable = new Hashtable<>();
// Thêm các phần tử vào Hashtable
hashtable.put("Key1", 100);
hashtable.put("Key2", 200);
hashtable.put("Key3", 300);

// In ra Hashtable
System.out.println("Hashtable: " + hashtable);
}
}

Output:
Hashtable: {Key3=300, Key2=200, Key1=100}
1.2. Thêm, Xóa, Chèn và Đọc Phần Tử Trong Hashtable
Để thực hiện các thao tác thêm, xóa, chèn và đọc phần tử trong Hashtable, chúng ta sử
dụng các phương thức của lớp Hashtable như put(), remove(), get() và containsKey()
import java.util.Hashtable;

public class Main {


public static void main(String[] args) {
Hashtable<String, Integer> hashtable = new Hashtable<>();

// Thêm phần tử vào Hashtable


hashtable.put("Key1", 100);
hashtable.put("Key2", 200);
hashtable.put("Key3", 300);

// Đọc phần tử từ Hashtable


int value = hashtable.get("Key2");
System.out.println("Value of Key2: " + value);

// Xóa phần tử từ Hashtable


hashtable.remove("Key1");
System.out.println("Hashtable after removal: " + hashtable);

// Kiểm tra sự tồn tại của một key trong Hashtable


boolean containsKey = hashtable.containsKey("Key3");
System.out.println("Contains Key3? " + containsKey);

// Chèn phần tử vào Hashtable nếu key chưa tồn tại


hashtable.putIfAbsent("Key4", 400);
System.out.println("Hashtable after putIfAbsent: " + hashtable);
}
}

Output:
Value of Key2: 200
Hashtable after removal: {Key3=300, Key2=200}
Contains Key3? true
Hashtable after putIfAbsent: {Key4=400, Key3=300, Key2=200}

1.3. Tìm Kiếm Thông Tin Trong Hashtable


Để tìm kiếm thông tin trong Hashtable, chúng ta có thể sử dụng phương thức get() để
lấy giá trị tương ứng với một key cho trước
int value = hashtable.get("Key2");
System.out.println("Value of Key2: " + value);

1.4. Sắp Xếp Các Phần Tử Trong Hashtable


Hashtable trong Java không hỗ trợ việc sắp xếp các phần tử theo thứ tự của chúng.
Tuy nhiên, nếu bạn muốn sắp xếp các phần tử dựa trên key hoặc value, bạn có thể
chuyển các entry của Hashtable vào một ArrayList và sắp xếp ArrayList đó.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

public class Main {


public static void main(String[] args) {
Hashtable<String, Integer> hashtable = new Hashtable<>();

hashtable.put("Key1", 100);
hashtable.put("Key3", 300);
hashtable.put("Key2", 200);

// Chuyển Hashtable thành List<Map.Entry<K, V>>


List<Map.Entry<String, Integer>> entryList = new
ArrayList<>(hashtable.entrySet());

// Sắp xếp List theo key (sử dụng Comparable)


Collections.sort(entryList, Map.Entry.comparingByKey());
System.out.println("Sorted Hashtable by Key: " + entryList);

// Sắp xếp List theo value (sử dụng Comparable)


Collections.sort(entryList, Map.Entry.comparingByValue());
System.out.println("Sorted Hashtable by Value: " + entryList);
}
}

Output:
Sorted Hashtable by Key: [Key1=100, Key2=200, Key3=300]
Sorted Hashtable by Value: [Key1=100, Key2=200, Key3=300]

You might also like