java字典类_Java字典类
java字典类
Java Dictionary is an . It was the parent class for any key-value mapping objects, such as Hashtable. However, it got deprecated in favor of the interface introduced in Java 1.2, which later on streamlined the . Dictionary doesn’t allow null for key and value.
Java词典是⼀个 。 它是任何键值映射对象(例如Hashtable)的⽗类。 但是,不赞成使⽤Java 1.2中引⼊的接⼝,该接⼝后来简化了 。字典不允许键和值为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 . 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,则抛出 。 如果键已经存在,则返回关联的值,然后更新新值。 如果是新密钥,则返回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 support , so we can specify the key-value types while declaring and instantiating the Dictionary object.
Dictionary⽀持 ,因此我们可以在声明和实例化Dictionary对象时指定键值类型。
带值的字典初始化 (Dictionary Initialization with Values)
The Hashtable class has a 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 .
字典是⼀个抽象类,⽽地图是⼀个 。
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.
这是⼀个简单的程序,我们在其中遍历键的枚举,以检查键是否存在于字典中。
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 = Element();
if (tEquals(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 = (lookupKey);
if(value != null)
System.out.println(lookupKey + " is present in the dictionary.");
else
System.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及其实现类。
java字典类

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。