java字典类

Java Dictionary is an abstract class. It was the parent class for any key-value mapping objects, such as Hashtable. However, it got deprecated in favor of the Map interface introduced in Java 1.2, which later on streamlined the Collections Framework. Dictionary doesn’t allow null for key and value.

Java词典是一个抽象类 。 它是任何键值映射对象(例如Hashtable)的父类。 但是,不赞成使用Java 1.2中引入的Map接口,该接口后来简化了Collections Framework 。 字典不允许键和值为null。

Note: Dictionary class has been obsolete and you shouldn’t use it. I use dictionary a lot in Python and got curious if there is a Dictionary class in Java? That’s how I got to know about the Dictionary class. The information provided here is just to have some idea about it if you are curious, try to avoid using it in your application.

注意 :Dictionary类已经过时了,您不应使用它。 我在Python中经常使用字典,并且对Java中是否有Dictionary类感到好奇? 这就是我对Dictionary类的了解。 如果您感到好奇,这里提供的信息只是对它有所了解,请尝试避免在应用程序中使用它。

Java字典方法 (Java Dictionary Methods)

This class declares 7 methods, which the implementation classes had to implement.

此类声明7个方法,实现类必须实现。

  1. int size(): returns the size of the dictionary.int size() :返回字典的大小。
  2. boolean isEmpty(): returns true if there are no key-value mappings, else false.boolean isEmpty() :如果没有键值映射,则返回true,否则返回false。
  3. Enumeration<K> keys(): returns the enumeration of the keys in the dictionary.Enumeration <K> keys() :返回字典中键的枚举。
  4. Enumeration<K> elements(): returns the enumeration of the values in the dictionary.Enumeration <K> elements() :返回字典中值的枚举。
  5. V get(Object key): returns the value associated with the key, if the key doesn’t exist then returns null.V get(Object key) :返回与键关联的值,如果键不存在,则返回null。
  6. V put(K key, V value): adds the key-value pair to the dictionary. If any of the key-value is null, then throws NullPointerException. If the key already exists, then the value associated is returned and then the new value is updated. If it’s a new key, then null is returned.V put(K key,V value) :将键值对添加到字典中。 如果任何键值为null,则抛出NullPointerException 。 如果键已经存在,则返回关联的值,然后更新新值。 如果是新密钥,则返回null。
  7. V remove(Object key): removes the key-value pair from the dictionary. The value associated with the key is returned. If the key doesn’t exist in the dictionary, then do nothing and null is returned.V remove(Object key) :从字典中删除键/值对。 返回与键关联的值。 如果键在字典中不存在,则不执行任何操作,并返回null。

词典实现类 (Dictionary Implementation Classes)

The only direct implementation of Dictionary is the Hashtable class. The Properties class extends Hashtable, so that is also an implementation of the Dictionary.

Dictionary的唯一直接实现是Hashtable类。 Properties类扩展了Hashtable,因此它也是Dictionary的实现。

Java字典初始化 (Java Dictionary Initialization)


Dictionary<String, Integer> dict = new Hashtable<>();

Dictionary support Generics, so we can specify the key-value types while declaring and instantiating the Dictionary object.

Dictionary支持Generics ,因此我们可以在声明和实例化Dictionary对象时指定键值类型。

带值的字典初始化 (Dictionary Initialization with Values)

The Hashtable class has a constructor that accepts a Map and copy its key-pair to the Hashtable object. We can use it to initialize a dictionary with values.

Hashtable类具有一个构造函数 ,该构造函数接受Map并将其密钥对复制到Hashtable对象。 我们可以使用它来初始化带有值的字典。


Map<String, String> tempMap = new HashMap<>();
tempMap.put("1", "One");Dictionary<String, String> dict1 = new Hashtable<>(tempMap);System.out.println(dict1); // prints {1=One}

Java字典与地图 (Java Dictionary vs Map)

  • Dictionary is an abstract class whereas Map is an interface.字典是一个抽象类,而地图是一个接口 。
  • Dictionary class has been deprecated when Collection classes were streamlined and Map got introduced in JDK 1.2在精简Collection类和在JDK 1.2中引入Map后,不推荐使用Dictionary类。
  • Don’t use Dictionary in your applications, it’s better to use Map.不要在应用程序中使用Dictionary,最好使用Map。

Java字典vs哈希表 (Java Dictionary vs Hashtable)

  • Dictionary is an abstract class where as Hashtable is the implementation of Dictionary.Dictionary是一个抽象类,其中Hashtable是Dictionary的实现。
  • Dictionary class has been deprecated whereas Hashtable is still being used. In fact, Hashtable is part of Collections framework and implements Map interface.不推荐使用字典类,而仍在使用Hashtable。 实际上,Hashtable是Collections框架的一部分,并实现Map接口。

如何检查字典中是否存在键 (How to Check if a Key Exists in Dictionary)

Here is a simple program where we are iterating over the enumeration of keys to check if the key exists in the dictionary or not.

这是一个简单的程序,我们在其中遍历键的枚举,以检查键是否存在于字典中。


Dictionary<String, String> dict = new Hashtable<>();
dict.put("1", "One");
dict.put("2", "Two");
dict.put("3", "Three");Enumeration<String> keys = dict.keys();boolean found = false;
String lookupKey = "2";
while (keys.hasMoreElements()) {String key = keys.nextElement();if (lookupKey.contentEquals(key)) {found = true;System.out.println(lookupKey + " is present in the dictionary.");break;}
}
if (!found)System.out.println(lookupKey + " is not present in the dictionary.");

We can also use the get() method to check if the key exists or not. If key doesn’t exists, then null is returned. Also, null values are not allowed, so it’s safe to use the null check for this.

我们还可以使用get()方法检查密钥是否存在。 如果键不存在,则返回null。 另外,不允许使用null值,因此使用null检查是安全的。


String value = dict.get(lookupKey);if(value != null)System.out.println(lookupKey + " is present in the dictionary.");
elseSystem.out.println(lookupKey + " is not present in the dictionary.");

结论 (Conclusion)

Dictionary is an obsolete class, so you shouldn’t use it in your application. You might be tempted to use it if you are coming from Python background, but avoid it and use Map and their implementation classes.

字典是一个过时的类,因此您不应在应用程序中使用它。 如果您来自Python背景,则可能会想使用它,但请避免使用它,并使用Map及其实现类。

翻译自: https://www.journaldev.com/39818/java-dictionary-class

java字典类

java字典类_Java字典类相关推荐

  1. java filereader类_Java FileReader类

    FileReader类从InputStreamReader类继承而来.该类按字符读取流中数据.可以通过以下几种构造方法创建需要的对象. 在给定从中读取数据的 File 的情况下创建一个新 FileRe ...

  2. java复用类_java复用类

    1. toString() 每一个非基本类型都有一个toString()方法:当编译器需要从对象获取一个string时,该对象的toString()方法就会被调用. 示例: class WaterSo ...

  3. java高级类_Java高级类特性(一)

    权限类内同包不同包子类不同包非子类 private √ × × × default √ √ × × protected √ √ √ × public √ √ √ √ 四.super关键字的使用 pac ...

  4. java 根据类名示例化类_Java即时类| from()方法与示例

    java 根据类名示例化类 即时类from()方法 (Instant Class from() method) from() method is available in java.time pack ...

  5. java 根据类名示例化类_Java即时类| EpochSecond()方法的示例

    java 根据类名示例化类 EpochSecond()方法的即时类 (Instant Class ofEpochSecond() method) Syntax: 句法: public static I ...

  6. java 大数类_Java大数类介绍

    java能处理大数的类有两个高精度大整数BigInteger和高精度浮点数BigDecimal,这两个类位于java.math包内,要使用它们必须在类前面引用该包:import java.math.B ...

  7. java 根据类名示例化类_Java即时类| plusMillis()方法与示例

    java 根据类名示例化类 即时类plusMillis()方法 (Instant Class plusMillis() method) plusMillis() method is available ...

  8. java 根据类名示例化类_Java LocalDateTime类| atOffset()方法与示例

    java 根据类名示例化类 LocalDateTime类atOffset()方法 (LocalDateTime Class atOffset() method) atOffset() method i ...

  9. java 根据类名示例化类_Java MathContext类| 带示例的getRoundingMode()方法

    java 根据类名示例化类 MathContext类的getRoundingMode()方法 (MathContext Class getRoundingMode() method) getRound ...

最新文章

  1. linux定时器多次,Spring 定时器执行两次
  2. 任正非最新署名文章:不要因为美国打压而放弃全球化战略
  3. python local global_python global与nonlocal关键字
  4. 【Cannot convert from [[B] to】 @RabbitListener 反序列化报错
  5. xpwifi热点设置android,教你在XP电脑中开启设置WiFi热点使用的步骤
  6. 【基础】优化背后的数学基础
  7. 计算机专业 美国,美国计算机专业的五大名校概况
  8. iOS 中可能用到的数学公式(绝对值、平方、取整、正余弦)
  9. 英语四六级考试系统+爬虫获取试题的系统(数据库设计)的开发思路
  10. 红包大战不再是两马战,内容平台为何成为新生力量?
  11. 3D建模和渲染吃CPU还是显卡?专业显卡和游戏显卡的区别
  12. Photoshop液化工具塑造完美的脸型
  13. P1345 [USACO5.4]奶牛的电信Telecowmunication
  14. IOI 2005 Riv 河流 题解
  15. Android中修改ScrollBar默认样式
  16. 零售行业业务流程自动化-零售行业RPA机器人流程自动化解决方案
  17. templateRef.createEmbeddedView is not a function
  18. 自动化测试之验证码处理
  19. HTML Rendering Error(This view has crashed)处理方法
  20. 医院病房管理信息系统PHP版本

热门文章

  1. linux下搭建python机器学习环境
  2. HDP Hive StorageHandler 下推优化的坑
  3. Android Studio使用JDBC远程连接mysql的注意事项(附示例)
  4. nodejs初探(四)实现一个多人聊天室
  5. UVA455 - Periodic Strings(周期串)
  6. [转载] python float()
  7. [转载] python win32api 使用小技巧
  8. [转载] Java基础知识面试题(2020最新版)
  9. day1-4js算术运算符及类型转化
  10. 20169212《Linux内核原理及分析》第十二周作业