Introduction to Java Dictionary Class
In java Dictionary, util.Dictionary is an abstract class that denotes a key-value storage repository and behaves like a map. If a key and some values are given, values can be stored in the object of the dictionary. After saving the value, it can be retrieved by using the key. That is why the dictionary is said to be working similarly to maps. Constructors, declaration, and further other details on the dictionary class will be discussed in the following sections.
Declaration
Below is the declaration of the dictionary class.
public abstract class Dictionary extends object
Constructors
The following is the only constructor of the Dictionary class.
Dictionary() : Sole constructor.
How does Java Dictionary class work?
As discussed above, dictionary class is an abstract class that behaves similarly to the map. If the key and certain values are provided, values can be saved in the dictionary object. After storing the value, it can be retrieved by using the key. Any non-null key and value can be used in this class.
Java Dictionary Class Methods
Let us see different methods of Dictionary class.
-
elements()
An enumeration will be returned for the values available in the dictionary.
Syntax:
public abstract Enumeration elements()
Example:
Code:
import java.util.*;
public class DictionaryExample
{
public static void main(String[] args)
{
// Dictionary initialization
Dictionary dict = new Hashtable();
// Map the keys to the values given using put() method
dict.put("99", "Sarah");
dict.put("82", "Elsa");
// Return the eneumeration of dictionary using elements() method
for (Enumeration e = dict.elements(); e.hasMoreElements();)
{
System.out.println("Values available in the dictionary : " + e.nextElement());
} }
}
Output:
Two elements are added to the dictionary, and the values of those keys are retrieved using the elements() method.
-
put(K key, V value)
The key mentioned will be mapped to the value given.
4.8 (9,016 ratings)
View Course
Syntax:
public abstract V put(K key, V value)
Example:
Code:
import java.util.*;
public class DictionaryExample
{
public static void main(String[] args)
{
// Dictionary initialization
Dictionary dict = new Hashtable();
// Map the keys to the values given using put() method
dict.put("101", "Anna");
dict.put("202", "Adam");
// Return the eneumeration of dictionary using elements() method
for (Enumeration e = dict.elements(); e.hasMoreElements();)
{
System.out.println("Values available in the dictionary : " + e.nextElement());
}
}
}
Output:
Two elements are added to the dictionary using put() methods, and the values of those keys are retrieved later.
-
remove(Object key)
The key and corresponding value will be removed from the dictionary.
Syntax:
public abstract V remove(Object key)
Example:
Code:
import java.util.*;
public class DictionaryExample
{
public static void main(String[] args)
{
// Dictionary initialization
Dictionary dict = new Hashtable();
// Map the keys to the values given using put() method
dict.put("99", "Sarah");
dict.put("82", "Elsa");
// Return the eneumeration of dictionary using elements() method
for (Enumeration e = dict.elements(); e.hasMoreElements();)
{
System.out.println("Values available in the dictionary : " + e.nextElement());
}
// remove the element 99 using remove() method
System.out.println(" Remove the element : " + dict.remove("99"));
// Return the eneumeration of dictionary using elements() method
for (Enumeration e = dict.elements(); e.hasMoreElements();)
{
System.out.println("Values available in the dictionary after removal: " + e.nextElement());
}
}
}
Output:
After adding two elements to the dictionary, one of them is removed using the remove() method.
-
keys()
An enumeration will be returned for the keys available in the dictionary.
Syntax:
public abstract Enumeration keys()
Example:
Code:
import java.util.*;
public class DictionaryExample
{
public static void main(String[] args)
{
// Dictionary initialization
Dictionary dict = new Hashtable();
// Map the keys to the values given using put() method
dict.put("101", "Anna");
dict.put("202", "Adam");
// Return the enumeration of dictionary using elements() method
for (Enumeration e = dict.keys(); e.hasMoreElements();)
{
System.out.println("Keys available in the dictionary : " + e.nextElement());
}
}
}
Output:
Two elements are added to the dictionary, and the keys are retrieved using the keys() method.
-
isEmpty()
Checks whether the dictionary maps no key-value. If no relation, true will be returned. Else, false.
Syntax:
public abstract booleanisEmpty()
Example:
Code:
import java.util.*;
public class DictionaryExample
{
public static void main(String[] args)
{
// Dictionary initialization
Dictionary dict = new Hashtable();
// Map the keys to the values given using put() method
dict.put("101", "Anna");
dict.put("202", "Adam");
// Checks no key-value pairs
System.out.println("Is there any no key-value pair : " + dict.isEmpty() + " \n " );
}
}
Output:
As there are key-value pairs in the dictionary, false will be returned when the isEmpty() method is called.
-
get(Object key)
A value will be returned, which maps the key in the dictionary.
Syntax:
public abstract V get(Object key)
Example:
Code:
import java.util.*;
public class DictionaryExample
{
public static void main(String[] args)
{
// Dictionary initialization
Dictionary dict = new Hashtable();
// Map the keys to the values given using put() method
dict.put("99", "Sarah");
dict.put("82", "Elsa");
// Return the eneumeration of dictionary using elements() method
for (Enumeration e = dict.elements(); e.hasMoreElements();)
{
System.out.println("Values available in the dictionary : " + e.nextElement());
}
System.out.println(" Remove the element : " + dict.remove("99"));
for (Enumeration e = dict.elements(); e.hasMoreElements();)
{
System.out.println("Values available in the dictionary after removal: " + e.nextElement());
}
System.out.println("The value of the key 82 is : " + dict.get("82"));
}
}
Output:
After adding two elements to the dictionary, one of them is retrieved using the get() method.
-
size()
The number of entries will be returned, which is available in the dictionary.
Syntax:
public abstract intsize()
Example:
Code:
import java.util.*;
public class DictionaryExample
{
public static void main(String[] args)
{
Dictionary dict = new Hashtable();
dict.put("99", "Sarah");
dict.put("82", "Elsa");
for (Enumeration e = dict.elements(); e.hasMoreElements();)
{
System.out.println("Values available in the dictionary : " + e.nextElement());
}
System.out.println("Dictionary size before removal of 99 is : " + dict.size());
// remove the element 99 using remove() method
System.out.println(" Remove the element : " + dict.remove("99"));
System.out.println("Dictionary size after removal of 99 is : " + dict.size());
}
}
Output:
The size of the dictionary is identified using the size() method before and after the removal of an element.
Conclusion
In this article, several aspects of Dictionary class, such as the declaration, constructors, working, and methods with examples is explained in detail.
Recommended Articles
This is a guide to Java Dictionary. Here we discuss the definition of Java Dictionary and How does the Java Dictionary Work with code implementation?. You may also have a look at the following articles to learn more –