Java dictionary is an inbuilt class that helps us to form different key-value relationships. It is an abstract class which extends the Object class and is present in the java.util package. The Dictionary class is an abstract parent of any class, such as the Hashtable, which maps the keys to values. Every key and every value is the object in anyone.
Java Dictionary Class
Java dictionary can also be termed as a list of pair values, where the pair consists of the key and the value. The relationship of the key to the values is somewhat similar to mapping. First, we map the key with the value, and then afterward, we can again retrieve the value with the help of the key.
Declaration
If we want to declare a Dictionary in java, we have to write the below method from java.util.Dictionary.keys().
public abstract Enumeration<K> keys()
Various Methods in Dictionary Packages
The following are the various methods that are predefined inside the java.util.Dictionary package.
S.No | Method | Description |
1. | put( key, value) |
The put() method takes two arguments as shown, and is used to map the key with the value and store it in the dictionary. Syntax: public abstract V put(K key, V value) Parameters: Return: |
2. | get() |
The get() method takes the key as the argument and returns the value that is mapped to it. If no value is mapped with the given key, it simply returns null. Syntax: public abstract V get(Object key) Parameters: Return: |
3. | elements() |
The elements() method is used to represent all the values present inside the Dictionary. It is usually used with loop statements as they can then represent one value at a time. Syntax: public abstract Enumeration elements() Return: |
4. | keys() |
As the elements() method returns the enumerated values present inside the dictionary; similarly, the keys() method returns the enumerated keys present inside the dictionary. Syntax: public abstract Enumeration keys() Return: |
5. | isEmpty() |
The isEmpty() method returns a boolean value, which is true if there are no key-value pairs present inside the Dictionary. If even any single key-value pair resides inside the dictionary, it returns false. Syntax: public abstract boolean isEmpty() Return: |
6. | remove(key) |
The remove() method takes the key as its argument, and it simply removes both the key and the value mapped with it from the dictionary. Syntax: public abstract V remove(Object key) Parameters: Return: |
7. | size() |
The size() method returns the total number of key-value pairs present inside the Dictionary. Syntax: public abstract int size() Return: |
The following program code is an example of using Dictionaries in Java.
import java.util.Dictionary; import java.util.Enumeration; import java.util.Hashtable; public class Dict { public static void main(String[] args) { Dictionary dictionary = new Hashtable(); // put method dictionary.put("Apple", "A fruit"); dictionary.put("Ball", "A round shaped toy"); dictionary.put("Car", "A four wheeler vehicle designed to accomodate usually four people"); dictionary.put("Dog", "An animal with four legs and one tail"); // get method System.out.println("\nApple: " + dictionary.get("Apple")); System.out.println("Dog: " + dictionary.get("Dog")); System.out.println("Elephant: " + dictionary.get("Elephant")); System.out.println(); // elements method for (Enumeration i = dictionary.elements(); i.hasMoreElements();) { System.out.println("Values contained in Dictionary : " + i.nextElement()); } System.out.println(); // keys method : for (Enumeration k = dictionary.keys(); k.hasMoreElements();) { System.out.println("Keys contianed in Dictionary : " + k.nextElement()); } // isEmpty method System.out.println("\nThe dictionary is empty? " + dictionary.isEmpty()); // remove method : dictionary.remove("Dog"); // Checking if the value is removed or not System.out.println("\nDog: " + dictionary.get("Dog")); // size method System.out.println("\nSize of Dictionary : " + dictionary.size()); } }
See the output.
Finally, Java Dictionary Class Example Tutorial is over.