简体   繁体   English

Dictionary中的Dictionary类和Map类有什么区别?

[英]What is the difference between the Dictionary class and the Map class in Java?

I was just seeing the difference between a HashMap and HashTable . 我刚刚看到HashMap和HashTable之间区别 Other than the obvious synchronized and unsynchronized difference, I noticed that HashTable extends the Dictionary class while HashMap implements Map . 除了显而易见的同步和非同步差异之外,我注意到HashTable扩展了Dictionary类,而HashMap实现了Map Both of them store <Key, Value> pair, so what's the difference. 它们都存储<Key, Value>对,所以有什么区别。 I tried Googling for it and I found few questions on SO ( 1 , 2 and 3 ). 我试着用搜索引擎的它,我发现SO(几个问题123 )。 I didn't find anything satisfactory other than the fact that Dictionary class is obsolete. 除了Dictionary类已经过时之外,我没有找到任何令人满意的东西。 Is there any difference between thee two classes? 这两个班级之间有什么区别吗? If no, why is the dictionary class obsolete now? 如果不是,为什么字典类现在已经过时了?

Dictionary is an abstract class that contains no non-abstract methods. Dictionary是一个不包含非抽象方法的抽象类。 Modern practice strongly recommends using an interface rather than a class in such a case. 现代实践强烈建议在这种情况下使用接口而不是类。 Map is such an interface. Map就是这样一个界面。

It also uses Enumeration which is also considered obsolete; 它也使用Enumeration ,它也被认为是过时的; Map uses Iterator which is not. Map使用Iterator而不是。

  • Dictionary is an abstract class whereas Map is an interface, hence it is more Java-idiomatic. Dictionary是一个抽象类,而Map是一个接口,因此它更像Java-idiomatic。
  • Dictionary was deprecated early in Java 1.2 and replaced by its better interface counterpart, Map . Dictionary 在Java 1.2早期被弃用 ,取而代之的是更好的接口对应Map This leads to believed that Dictionary was just a bad design choice and the Java folks kept the Dictionary class for backward compatibility. 这导致相信Dictionary只是一个糟糕的设计选择,Java人员保持Dictionary类的向后兼容性。

The main difference is that Dictionary is an abstract class and Map is a interface. 主要区别在于Dictionary是一个抽象类,Map是一个接口。 Collections API uses interfaces like Map, List, Set. Collections API使用Map,List,Set等接口。

Dictionary is an abstract base class of Hashtable. Dictionary是Hashtable的抽象基类。 Both are still in JDK for backwards compatibility with old code. 为了向后兼容旧代码,两者仍然在JDK中。 We are expected to use HashMap and other implementations of Map interface introduced in Java 1.2. 我们期望使用HashMap和Java 1.2中引入的Map接口的其他实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM