GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — Java Dictionary Class
Selected Reading
Java Dictionary ClassIntroductionThe Java Dictionary class is the abstract parent of any class, such as Hashtable, which maps keys to values. The Dictionary class was part of the early Java platform since JDK 1.0. CharactersticsThe following are the important points about the Dictionary class in Java −
Class DeclarationThe following is the declaration for java.util.Dictionary class − public abstract class Dictionary<K,V> extends Object Parameters
Why was the Dictionary Class Replaced?The Dictionary class is declared obsolete in its documentation with the introduction of the Java Collections Framework in Java 1.2. This change was introduced to handle groups of objects more easily, and the Collections Framework provided it with a hierarchical structure under interfaces in Java. The main reason for its depreciation is the Map interface. The Map interface provided different implementations like HashMap, TreeMap, or LinkedHashMap, and as the Dictionary class is dependent on the abstract class, it doesn't provide other implementations. The Dictionary class is dependent on enumeration for the iteration of key-value pairs, but in the Map interface iterator class was introduced, which is more efficient in iterating as well as removing elements during iteration which is not supported in the Dictionary Class. Creating a Dictionary ClassTo create a Dictionary class in Java, follow the steps as follows − Step 1The first and important step is to import the java.util.Dictionary and java.util.Hashtable at the start of the code, as without the Dictionary and Hashtable classes, we cannot use their methods. import java.util.Dictionary; import java.util.Hashtable; Step 2The second step is to create the object of the Dictionary class. Here, the "dict" is the Dictionary class object, as we know that it is an abstract class, so to instantiate it, we use a hashtable for its creation. Dictionary<Integer, String> dict = new Hashtable<>(); The above Dictionary consists of keys as Integers and values as strings. Step 3The third step is to add key-value pairs using the put() method. We will be adding two entries in the dict as (10, "Orange") and (20, "Mango"). dict.put(10, "Orange"); dict.put(20, "Mango"); ExampleBelow is an example to create a Dictionary in Java − package com.tutorialspoint;
import java.util.Dictionary;
import java.util.Hashtable;
public class DictionaryDemo {
public static void main(String[] args) {
// creating a dictionary with a hashtable
Dictionary<Integer, String> dict = new Hashtable<>();
// adding pairs using the put() method
dict.put(10, "Orange");
dict.put(20, "Mango");
// printing the dictionary
System.out.println(dict);
}
}
OutputLet us compile and run the above program. This will produce the following result − {10=Orange, 20=Mango}
Class ConstructorsThe following is the class constructor supported by the Dictionary class in Java −
Class MethodsThe following are the class methods supported by the Dictionary class in Java −
Methods InheritedThe Dictionary class inherits methods from the following classes −
Adding and Iterating Mappings in the DictionaryThe Java Dictionary put(K, V) method is used to add a key-value pair. We're creating a dictionary instance using a Hashtable object of Integer, Integer. Then we've added a few elements to it using the put(K, V) method. An enumeration is retrieved using the elements() method, and the enumeration is then iterated to print the elements of the dictionary. ExampleBelow is an example of adding pairs to a Dictionary and iterating over them using enumeration in Java − package com.tutorialspoint;
import java.util.Enumeration;
import java.util.Dictionary;
import java.util.Hashtable;
public class DictionaryDemo {
public static void main(String[] args) {
// creating a dictionary with a hashtable
Dictionary<Integer, Integer> dictionary = new Hashtable<>();
// add 2 pairs using the put() method
dictionary.put(1, 1);
dictionary.put(2, 2);
// using the enumeration for iterating over the pairs
Enumeration<Integer> enumeration = dictionary.elements();
while(enumeration.hasMoreElements()) {
System.out.println(enumeration.nextElement());
}
}
}
OutputLet us compile and run the above program. This will produce the following result − 2 1 Removing a Mapping from the DictionaryThe Java Dictionary remove(K) method is used to remove a key-value pair. We're creating a dictionary instance using a Hashtable object of Integer, Integer. Then we've added a few elements to it using the put(K, V) method. Then, using the remove() method, we have removed one mapping and printed the dictionary. ExampleBelow is an example of removing a pair from a Dictionary in Java − package com.tutorialspoint;
import java.util.Dictionary;
import java.util.Hashtable;
public class DictionaryDemo {
public static void main(String[] args) {
// creating a dictionary with a hashtable
Dictionary<Integer, Integer> dictionary = new Hashtable<>();
// add 3 pairs using the put() method
dictionary.put(1, 1);
dictionary.put(2, 2);
dictionary.put(3, 3);
// using the remove() method
dictionary.remove(1);
System.out.println(dictionary);
}
}
OutputLet us compile and run the above program. This will produce the following result − {3=3, 2=2}
Advertisements
| ||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |