понеділок, 2 лютого 2015 р.

Java Dictionary example

The Java Dictionary class is an abstract class that allows all its derived classes to store data in the form of key-value pairs. Both keys and values can be objects of any type, but keys should be unique. Also, both keys and values cannot be null. For instance, if a key or value is referring to null and then inserted into a dictionary, a NullPointerException error occurs. Therefore, it’s mandatory to provide a non-null value for storing both keys and values.



How is the Java Dictionary Class Implemented?

The Java Dictionary class has now become obsolete and currently all the collection types that store data in the form of key-value pairs implement the Java Map interface. However, the Java Dictionary class variables can still be used to store a reference to the Hashtable classes implementing the Map interface. The three major classes that implement the Map interface and stores data in the form of key-value pair are as follows:

    Hashtable
    HashMap
    LinkedHashMap

Java Dictionary Examples

This section contains brief examples of Hashtable, HashMap and LinkedHashMap classes that store values in the form of key-value pairs similar to a dictionary collection. The Hashtable object is stored in the Dictionary type variable since it inherits directly from the Dictionary. But as previously stated, the Dictionary class has been deprecated in Java and now collections that store data in the form of key-value pair implement the new Map interface; therefore, the HashMap and LinkedHashMap objects would be stored in the Map type variable as seen in the following examples. The first example demonstrates the Hashtable implementation of the Dictionary class. The code is demonstrated below:

Hashtable:

Dictionary  cities = new Hashtable();
cities.put("New York", "USA");
cities.put("Toronto", "Canada");
cities.put("Manchester", "UK");
cities.put("Berlin", "Germany");

for (Enumeration city = cities.keys(); city.hasMoreElements();) {
String key = (String)city.nextElement();
System.out.println("Key: "+key +" Value: " + cities.get(key)); }

HashMap

HashMap is similar in its functionality to the Hashtable as both of them store collections of data in the form of key-value pairs. The difference between HashMap and Hashtable collection is that Hashtable implements  both the Dictionary and Map interface as well. The HashMap class only implements the Map interface and does not directly inherit from the Dictionary class. The following example demonstrates how a HashMap can be used to store a key-value pair with the same keys and values that were stored in the Hashtable in the last example:

Map  cities = new HashMap();
cities.put("Newyork", "USA");
cities.put("Toronto", "Canada");
cities.put("Manchester", "UK");
cities.put("Berlin", "Germany");
Set citykeys = cities.keySet();

for (Iterator city = citykeys.iterator(); city.hasNext();) {
String key = (String)city.next();
System.out.println("Key: "+key +" Value: "+cities.get(key));}

The implementation for LinkedHashMap is similar except, replace the
constructor of HashMap with LinkedHashMap in the last example. The
output would also be similar.

The information was taken from here

Немає коментарів:

Дописати коментар