Java 中 Dictionary 类的重要性?

javaobject oriented programmingprogramming更新于 2024/7/25 19:53:00

Dictionary  类是一个抽象类,它表示一个 键/值 对,其操作方式与 Map  类似,并且它是 Java 中的遗留类Dictionary  类有两个重要方法 Dictionary.keys()Dictionary.elements(),它们可以通过 Enumeration 进行迭代。Dictionary 类的其他重要方法是 isEmpty()get()remove()size()

语法

public abstract class Dictionary<K,V> extends Object

示例

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());
      }
   }
}

输出

3
2
1
Raja
Jaidev
Adithya

相关文章