×

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

The Diamond Operator in Java 7

The Diamond operator is a comparatively recent Java operator originally developed in JDK 7 to enhance type identification and eliminate boilerplate Java code. The Diamond operator is characterized by a...

2 minutes read.

Java Enumeration

In a computer language, enumerations express a set of named constants. For instance, the four suits in a deck of playing cards could be represented by the enumerators Club, Diamond,...

4 minutes read.

Best Practices to use String Class in Java

Use String Builder or String Buffer for String concatenation in place of + operator.Compare two strings by equals( ) method instead == operator.Call .equals( ) method on a known String...

3 minutes read.

HashMap Vs HashTable

HashMap HashMap is the basic implementation of the map interface in Java. HashMap stores the data in key and value pairs. Keys are used to access the value of the element. It...

5 minutes read.

XOR of Array Elements Except Itself in Java

A lot of the biggest IT organisations, including The Google, Amazon, TCS, and The Accenture, etc., question about this issue in their interview processes. By addressing the issue, one hopes to assess...

5 minutes read.

Sliding Window Problem in Java

A sliding window is used in computer science and data science to process large datasets. It involves breaking the dataset into smaller chunks or windows and then processing it in...

6 minutes read.

Java Byte Code

Java byte code is really a powerful mechanism which makes Java a portable and platform-independent programming language. There are two software components which go along and make this byte code...

3 minutes read.

How to override toString() method in Java?

Java is an object-oriented language. It only works with classes and objects. Thus, whenever we need to calculate, we need an object or objects that belong to the class. The Java method...

2 minutes read.

Java Math abs() Method

The abs() method of Math class returns the absolute value of the argument where the argument can be int, double, float, long. Syntax public static int abs(int a) public static float abs(float a) public...

2 minutes read.

Static Array in Java

In this tutorial, we will study static arrays in Java. An array is a data structure that is of great importance in any programming language. It is classified into two...

3 minutes read.

Java Swing Time Picker

Prerequisites In this tutorial, we will learn the time picker in java swing. Before learn time picker, we should learn about java swing. Java Swing Introduction The Java Foundation Classes include Java Swing....

5 minutes read.

Java Boolean compare() method

The compare() method of Java Boolean class compares the specified Boolean values and returns a positive 1 or negative 1 or zero integer value based on the result. Syntax public static int...

2 minutes read.

Binary Search Java

Binary search is a search mechanism for key elements from the given List/Array. In Binary search, the search mechanism is followed by dividing the array into parts; hence the search...

3 minutes read.

Differences between Set and List in Java

Set in Java: The Java. util package contains an interface called the set. The set interface expands the Collection interface. A collection interface is an unordered collection of List where duplicates...

6 minutes read.

Automorphic Number Program in Java

We will learn about automorphic numbers through examples in this article, and we'll also make Java programmes that can determine whether a specific number was automorphic or not. What is an...

3 minutes read.

Java HashMap

Java HashMap extends AbstractMap and implements Map interface. It is the collection of multiple entries where an entry consists of key and value pair. The HashMap can contain only one...

7 minutes read.

Comparator vs Comparable in Java

Comparator Vs Comparable in Java In Java, sorting of primitive data types can be done using different inbuilt functions that are available. But for sorting the collection of objects or types...

4 minutes read.

Java Regular Expressions

Java Regular Expressions The Java Regex or Regular Expression is an API that defines a pattern for searching or manipulating strings. A regular expression is a pattern that can be as simple as...

8 minutes read.

Static vs Non-Static in Java

A static method can access and update the value of a static data member. The static keyword is mostly used in Java for memory management. The static keyword can be...

6 minutes read.

Packages in Java

Packages in Java can be defined as an assortment for grouping various classes and interfaces based on their performance. It is a catalog for holding various java files. They provide...

4 minutes read.