A Từ điển lớp là một lớp trừu tượng đại diện cho một khóa / giá trị ghép nối và hoạt động giống như một Bản đồ và nó là lớp kế thừa trong Java. A Từ điển lớp có hai phương thức quan trọng Dictionary.keys () và Dictionary.elements () t mũ có thể được lặp lại bởi một Liệt kê . Các phương thức quan trọng khác của lớp Từ điển là isEmpty () , get () , loại bỏ () và size () .
Cú pháp
public abstract class Dictionary<K,V> extends Object
Ví dụ
import java.util.*;
public class DictionaryTest {
public static void main(String[] args) {
Dictionary<Integer, String> dic = new Hashtable<Integer, String>();
dic.put(1, "Adithya");
dic.put(2, "Jaidev");
dic.put(3, "Raja");
Enumeration<Integer> key = dic.keys();
while(key.hasMoreElements()) {
System.out.println(key.nextElement());
}
Enumeration<String> element = dic.elements();
while(element.hasMoreElements()) {
System.out.println(element.nextElement());
}
}
} Đầu ra
3 2 1 Raja Jaidev Adithya