The Dictionary class

In Java, a Dictionary is an abstract class that maps keys to values. Both keys and values can be objects of any type but not null. An attempt to insert either a null key or a null value to a dictionary causes a NullPointerException exception. Therefore, it’s mandatory to provide a non-null value for both keys and values.

Abstract Methods defined by Dictionary Class:

A Dictionary has the form Dictionary<K, V> where:

  • K : is the type of keys.
  • V : is the type of values.

Method : get()

// Get the value associated with a key
public abstract V get(Object key);

Note: Thorws NullPointerException - if the key is null.

Method : isEmpty()

// Check if the dictionary has no key
public abstract boolean isEmpty();

Note: True is returned if the dictionary contains no entries.

Method : put()

// Set the value associated with a key
public abstract V put(K key, V value);

If this dictionary already contains an entry for the specified key, the value already in this dictionary for that key is returned, after modifying the entry to contain the new element. If this dictionary does not already have an entry for the specified key, an entry is created for the specified key and value, and null is returned.

Note: Neither the key nor the value can be null, otherwise it throws NullPointerException.

Method : remove()

// Remove a key from the dictionary
public abstract V remove(Object key);

It returns the value to which the key is mapped, or return null if the key did not have a mapping.

Note: True is returned if the dictionary contains no entries.

Method : size()

// Get the number of key-value pairs in the dictionary
public abstract int size();

Method : elements() & Key()

// Returns an enumeration of the keys
public abstract Enumeration<K> keys()

// Returns an enumeration of the values
public abstract Enumeration<V> elements()

The Dictionary class offers three views: a set of keys using the keySet() method, a collection of values using the values() method and a set of key-value mappings using the entrySet() method.

Data Structures - ArrayList
Data Structures - ArrayList
Previous Article
HashTable class
HashTable class
Next Article
Lakshay Sharma
I’M LAKSHAY SHARMA AND I’M A FULL-STACK TEST AUTOMATION ENGINEER. Have passed 16 years playing with automation in mammoth projects like O2 (UK), Sprint (US), TD Bank (CA), Canadian Tire (CA), NHS (UK) & ASOS(UK). Currently, I am working with RABO Bank as a Chapter Lead QA. I am passionate about designing Automation Frameworks that follow OOPS concepts and Design patterns.
Reviewers
Virender Singh's Photo
Virender Singh

Similar Articles

Feedback