Java Dictionary - Dictionary in Java - Walking Techie

Blog about Java programming, Design Pattern, and Data Structure.

Wednesday, March 15, 2017

Java Dictionary - Dictionary in Java

What is Dictionary in Java?

Dictionary is an abstract class that represents a key/value storage repository and operates much like Map.

Given a key and value, you can store the value in a Dictionary object. Once the value is stored, you can retrieve it by using its key. Thus, like a map, a dictionary can be thought of as a list of key/value pairs. Although not currently deprecated, Dictionary is classified as obsolete, because it is fully superseded by Map.

Here is the topic that will cover in this post.

  1. Dictionary class Declaration
  2. Dictionary constructors
  3. Dictionary class Methods
  4. Example

Java Dictionary class Declaration

Java Dictionary is an abstract class you need to instantiate a concrete implementation of the abstract class in order to use it. Hashtable classe provide implementations of the Dictionary abstract class. With the advent of JDK 5, Dictionary was made generic. It is declared as shown here:

public abstract class Dictionary<K,V>

Here, K specifies the type of keys, and V specifies the type of values.

Java Dictionary constructors

Dictionary has only one default constructor.

public Dictionary() { }

Java Dictionary methods

Method Description
abstract public int size() Returns the number of entries (distinct keys) in this dictionary.
abstract public boolean isEmpty() Returns true if the dictionary is empty, and returns false if it contains at least one key.
abstract public Enumeration<K> keys() Returns an enumeration of the keys contained in the dictionary.
abstract public Enumeration<V> elements() Returns an enumeration of the values contained in the dictionary.
abstract public V get(Object key) Returns the object that contains the value associated with key. If key is not in the dictionary, a null object is returned.
abstract public V put(K key, V value) Inserts a key and its value into the dictionary. Returns null if key is not already in the dictionary; returns the previous value associated with key if key is already in the dictionary.
abstract public V remove(Object key) Removes key and its value. Returns the value associated with key. If key is not in the dictionary, a null is returned.

Example

Here is a simple program of Dictionary. Hashtable is concrete implementation of Dictionary.

package com.walking.techie;

import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;

public class DictionaryDemo {

  public static void main(String[] args) {
    Dictionary<Integer, String> dictionary = new Hashtable<>();
    dictionary.put(424, "Failed Dependency");
    dictionary.put(422, "Unprocessable Entity");
    dictionary.put(429, "Too Many Requests");
    dictionary.put(504, "Gateway Timeout");
    dictionary.put(409, "Conflict");

    System.out.println("Value in dictionary corresponding to 429 : " + dictionary.get(429));
    System.out.println("Value in dictionary corresponding to 202 : " + dictionary.get(202));

    System.out.println("Is dictionary empty? : " + dictionary.isEmpty());
    System.out.println(
        "remove key 504 and its value from dictionary : value : " + dictionary.remove(504));
    System.out.println("Size of the dictionary is : " + dictionary.size());

    System.out.println();
    // enumerate keys of dictionary
    Enumeration<Integer> keys = dictionary.keys();
    while (keys.hasMoreElements()){
      Integer key = keys.nextElement();
      System.out.println("Key : "+key +" value : "+dictionary.get(key));
    }

    System.out.println();
    //enumerate values of dictionary
    Enumeration<String> values = dictionary.elements();
    while (values.hasMoreElements()){
      String value = values.nextElement();
      System.out.println("value : "+value);
    }
  }
}

Output of above program is shown below:

Value in dictionary corresponding to 429 : Too Many Requests
Value in dictionary corresponding to 202 : null
Is dictionary empty? : false
remove key 504 and its value from dictionary : value : Gateway Timeout
Size of the dictionary is : 4

Key : 424 value : Failed Dependency
Key : 422 value : Unprocessable Entity
Key : 409 value : Conflict
Key : 429 value : Too Many Requests

value : Failed Dependency
value : Unprocessable Entity
value : Conflict
value : Too Many Requests

Note: The Dictionary class is obsolete. You should implement the Map interface to obtain key/value storage functionality.

No comments :

Post a Comment