Dictionary là một abstract class đại diện cho cặp khóa/giá trị và hoạt động như một Map, đây là một lớp kế thừa (legacy class) trong Java. Lớp Dictionary có hai phương thức quan trọng Dictionary.keys() và Dictionary.elements() có thể lặp bằng Enumeration. Các phương thức quan trọng khác: isEmpty(), get(), remove() 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());
}
}
}
Kết quả
3 2 1 Raja Jaidev Adithya