×

Dictionary in Java

Dictionary in Java

The Dictionary class represents a key-value relation which maps keys to values. In Dictionary class, every key and every value is an object.

It is an abstract class associated with Java since JDK 1.0. In Dictionary, every key is associated with at most one value and any object that contains some value can be used as a key and as a value.

Dictionary in Java

The Dictionary class is deprecated, so new implementation should implement the Map interface, rather than extending this class.

Constructor for Dictionary class:

public Dictionary()

Methods of Dictionary Class

Modifiers Methods Description
      abstract v get(Object key) This method returns the associated value for the specified key in the argument. Otherwise, returns null.
put(K key, V value) It maps the specified key to the specified value in the argument.
remove(Object key) This removes the specified key and its corresponding value from the dictionary. It will do nothing if the specified key is not available in the dictionary.
abstract boolean isEmpty() It tests whether the dictionary maps keys to the value or not. The result will be true if and only if the dictionary is blank.
abstract Emumeration<V> elements() This method returns an enumeration that will generate all the values contained in the entries of the Dictionary.
keys() This method returns a list of the keys present in the dictionary.
abstract int size() It returns the number of entries (key-value) in the Dictionary.

Example to illustrate all the above methods:

import Java.util.*;
class DictionaryDemo
{
    public static void main(String[] args)
    {
        //creating a Dictionary
        Dictionary d = new Hashtable();
        // put() method to insert values along with the key
        d.put("100", "Tutorials");
        d.put("101", "Examples");
        // elements() method for emumeration of values:
       System.out.println( "Elements in Dictionary are : ");
        for (Enumeration i = d.elements(); i.hasMoreElements();)
        {
            System.out.println(i.nextElement());
        }
        // get() method to fetch values from the dictionary :
        System.out.println("\nValue at key = 50 : " + d.get("50"));
        System.out.println("Value at key = 100 : " + d.get("100"));
        // isEmpty() method to check if the dictionary is empty
        System.out.println("\nThere is no key-value pair : " + d.isEmpty() + "\n");
        // keys() method to get all the keys from the dictionary
        for (Enumeration k = d.keys(); k.hasMoreElements();)
        {
            System.out.println("Keys in Dictionary are: " + k.nextElement());
        }
        // remove() method to remove the element at 100 key position
        System.out.println("\nRemove : " + d.remove("100"));
        System.out.println("Checking the value of the removed key : " + d.get("100"));
        System.out.println("\nSize of Dictionary is : " + d.size());
    }
} 

Output:

Elements in Dictionary are :
Examples
Tutorials
Value at key = 50 : null
Value at key = 100 : Tutorials
There is no key-value pair : false
Keys in Dictionary are: 101
Keys in Dictionary are: 100
Remove : Tutorials
Checking the value of the removed key : null
Size of Dictionary is : 1 

Related Topics

Frugal number in Java

A frugal number is a positive integer with base b that has more digits than the number of prime factors it can be factored into (including exponents greater than 1)....

3 minutes read.

Short Circuit Logical Operators in Java

When there are two or more relational expressions in a decision-making statement, logical operators are utilized to combine them. The logical operators short circuit and not-short circuit fall into two...

5 minutes read.

Java Math with Methods and Examples

Java Math class contains various methods for performing math operations like min(), max(), avg() and various trigonometric functions like sin(), cos(), tan() etc. Methods: The java.lang.Math class contains various methods for performing...

5 minutes read.

Convert List to String in Java

We occasionally need to create a string from a list of characters. Since a string is only a collection of characters, a character array can be converted into a string....

6 minutes read.

URLConnection Class

What is the URL? URL stands for Uniform Resource Locator, is used to specify addresses on the World Wide Web. A URL relates to the identification of any resource connected to the web. URL syntax: Protocol://hostname/other_information(files...

6 minutes read.

Java Math getExponent() Method

The getExponent() method of Math class returns the unbiased exponent of the argument. Syntax: public static int getExponent (double d) Parameters: The parameter ‘d’ represents the double value. Return Value: The getExponent () method returns the...

1 minute read.

Difference Between Thread.start() and Thread.run()

In the Java programming language, the multi-threading concept consists of the start() and run() methods. Thread.start(): The thread's execution is initiated by invoking the start() method. The start() method operates two threads...

4 minutes read.

Heart Pattern in Java

Heart Pattern is yet another intricate pattern program, however, due to its complexity, interviewers hardly ever inquire about it. Method for Printing the Heart Number Pattern Put the value of the total row...

2 minutes read.

Java Code Optimization

We encounter the idea of optimization while working on any Java application. It is essential that the code we write is not only clear and error-free but also optimized, meaning...

9 minutes read.

Byte to Hex in Java

Java exclusively uses byte data types to store in a byte array, which is an array. Each component of a byte array has a default value of 0. Hex String -...

3 minutes read.

Composition in Java

Composition Java uses the composition method to implement a has-a connection. Composition allows us to reuse code in the same way that Java inheritance does. The "is-a" relationship is implemented using the...

3 minutes read.

How to increment and decrement date using Java?

Before understanding how to increment and decrement the date, one must know about the Calendar class in Java. The Java calendar class offers methods for converting dates between a given moment...

3 minutes read.

Difference between throw and throws in java

This article shows you the core difference between “throw” and “throws”keywords in Java programming language.The throw keyword tells Java you want another part of the code to deal withthe exception,...

2 minutes read.

Arithmetic Operations on String in Java

Introduction Arithmetic, Relational, Bitwise, and Logical operators are all available in Java. Simple mathematical calculations are performed using Java arithmetic operators. Basic Arithmetic operators are considered in Java to be Addition,...

4 minutes read.

JDBC Program in Java

JDBC Program in Java JDBC is an API that defines how a client may access a database. It is a part of Java Standard Edition (Java SE). JDBC stands for Java...

4 minutes read.

Number Pattern Programs in Java

Number Pattern Programs in Java: Number pattern programs are part of pattern programs. In the previous section, we have learned the approach to print the pattern program in Java. To...

6 minutes read.

Sealed Class in Java

What is a Sealed Class in Java? In programming, the two main issues that must be taken into account when creating an application are security and control flow. The use of...

5 minutes read.

ArrayDeque in Java

ArrayDeque The ArrayDeque is one of the essential concepts in java to implement the deque interface. It will allow us to apply a resizable array to implement the Deque interface. This...

8 minutes read.

How to find length of integer in Java

We can find the length of the integer in many ways. The length of an integer is defined as the count of the number of digits for the given integer. These...

5 minutes read.

Difference Between Data Hiding and Abstraction in Java

Abstraction: Data Abstraction and Data Hiding ideas are utilised to show the expected data to the end client and conceal the superfluous subtleties, however, for specific purposes like decreasing the framework's...

10 minutes read.