AppDividend
Latest Code Tutorials

Java Dictionary Class Example | Dictionary Class In Java Tutorial

0

Java Dictionary Class Example | Dictionary Class In Java Tutorial is today’s topic. In java, we have a class Dictionary which helps us to form different key-value relationships. It is an abstract class which extends the Object class and is present in the java.util package. The Dictionary class is an abstract parent of any class, such as the Hashtable, which maps the keys to values. Every key and every value is the object in anyone.

Java Dictionary Class

Dictionary can also be termed as a list of pair values, where the pair consists of the key and the value. The relationship of the key to the values is somewhat similar to mapping. First, we map the key with the value, and then afterward, we can again retrieve the value with the help of the key.

Declaration

If we want to declare a Dictionary in java, we have to write the below method from java.util.Dictionary.keys().

public abstract Enumeration<K> keys()

Various Methods in Dictionary Packages

The following are the various methods that are predefined inside the java.util.Dictionary package.

S.No Method Description
1. put( key, value) 

The put() method takes two arguments as shown, and is used to map the key with the value and store it in the dictionary.

Syntax: public abstract V put(K key, V value)

Parameters:
-> key
-> value

Return:
key-value pair mapped in the dictionary.

2. get()

The get() method takes the key as the argument and returns the value that is mapped to it. If no value is mapped with the given key, it simply returns null.

Syntax: public abstract V get(Object key)

Parameters:
key – key whose mapped value we want

Return:
value mapped with the argument key.

3. elements()

The elements() method is used to represent all the values present inside the Dictionary. It is usually used with loop statements as they can then represent one value at a time.

Syntax: public abstract Enumeration elements()

Return:
value enumeration in the dictionary.

4.  keys()

As the elements() method returns the enumerated values present inside the dictionary; similarly, the keys() method returns the enumerated keys present inside the dictionary.

Syntax: public abstract Enumeration keys()

Return:
The key enumeration in the dictionary.

5.  isEmpty()

The isEmpty() method returns a boolean value, which is true if there are no key-value pairs present inside the Dictionary. If even any single key-value pair resides inside the dictionary, it returns false.

Syntax: public abstract boolean isEmpty()

Return:
It returns true if there is no key-value relation in the dictionary; else false.

6. remove(key)

The remove() method takes the key as its argument, and it simply removes both the key and the value mapped with it from the dictionary.

Syntax: public abstract V remove(Object key)

Parameters:
key: a key to be removed

Return:
The key enumeration in the dictionary.

7. size()

The size() method returns the total number of key-value pairs present inside the Dictionary.

Syntax: public abstract int size()

Return:
It returns the no. of key-value pairs in the Dictionary.

 

The following program code is an example of using Dictionaries in Java.

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

public class Dict {
  public static void main(String[] args) {

    Dictionary dictionary = new Hashtable();

    // put method
    dictionary.put("Apple", "A fruit");
    dictionary.put("Ball", "A round shaped toy");
    dictionary.put("Car", "A four wheeler vehicle designed to accomodate usually four people");
    dictionary.put("Dog", "An animal with four legs and one tail");

    // get method
    System.out.println("\nApple: " + dictionary.get("Apple"));
    System.out.println("Dog: " + dictionary.get("Dog"));
    System.out.println("Elephant: " + dictionary.get("Elephant"));
    System.out.println();

    // elements method
    for (Enumeration i = dictionary.elements(); i.hasMoreElements();) {
      System.out.println("Values contained in Dictionary : " + i.nextElement());
    }
    System.out.println();

    // keys method :
    for (Enumeration k = dictionary.keys(); k.hasMoreElements();) {
      System.out.println("Keys contianed in Dictionary : " + k.nextElement());
    }

    // isEmpty method
    System.out.println("\nThe dictionary is empty? " + dictionary.isEmpty());

    // remove method :
    dictionary.remove("Dog");

    // Checking if the value is removed or not
    System.out.println("\nDog: " + dictionary.get("Dog"));

    // size method
    System.out.println("\nSize of Dictionary : " + dictionary.size());
  }
}

See the output.

 

Java Dictionary Class Example

Finally, Java Dictionary Class Example | Dictionary Class In Java Tutorial is over.

Recommended Posts

Nested class In Java Example | Java Nested Class Tutorial

Java Type Casting Example | Type Casting In Java Tutorial

Java Interface Example | Interface in Java Tutorial For Beginners

HashMap in Java Tutorial With Example

Java ArrayList Example | ArrayList in Java Tutorial

Java Scanner Class Tutorial With Example | Scanner Class in Java

Java Constructor Tutorial With Example | Constructor in Java

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.