Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Methods/Functions
2.1.
Example:
3.
Frequently Asked Questions
3.1.
What are HashTables, and how is it different from a dictionary?
3.2.
What are iterators and enumerations?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Dictionary in Java

Author Yashesvinee V
0 upvote
gp-icon
Data structures & algorithms (Beginner to Intermediate)
Free guided path
13 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction

Dictionary in Java is an abstract class that is a part of java.util package. It stores key-value pairs and works very similar to a Map. The Dictionary class is a direct superclass of HashTable, implemented by the Map interface. (See Data Structures)

Every key or value of a dictionary is an object and must not be null. Dictionary key objects have at most one value associated with them, using which elements can be accessed. Dictionary() is the sole constructor of the Dictionary class.

Declaration:

public abstract class Dictionary extends Object

 

Initialisation:

Dictionary dict_name = new Hashtable(); 

Methods/Functions

Method

Type

Description

isEmpty( ) abstract Boolean It checks if the dictionary has no key-value pairs.
put( K key, V value ) abstract V It maps the specified key and value in the dictionary and returns the previous value of the key or null if absent.
size( ) abstract int It returns the number of entries in the dictionary.
get( Object key ) abstract V It returns the value of the specified key.
remove( Object key ) abstract V It removes the specified key and its value from the dictionary and returns the value or null if absent.
keys( ) abstract Enumeration<K> It returns an enumeration of all the keys in the dictionary.
elements( ) abstract Enumeration<V> It returns an enumeration of all the values in the dictionary.

Also read, Duck Number in Java and  Hashcode Method in Java

Example:

The following example shows the use of all the functions of the Dictionary class mentioned in the table.

import java.util.*;
public class Main
{
  public static void main(String[] args)
  {    
    Dictionary dict = new Hashtable();

    dict.put("A", "Australia");
    dict.put("E", "Europe");

    Enumeration continent = dict.elements();
    System.out.print("Keys in Dictionary : ");
    while(continent.hasMoreElements())
      System.out.print(continent.nextElement()+ "  ");

    Enumeration letter = dict.keys();
    System.out.print("\nValues in Dictionary : ");
    while(letter.hasMoreElements())
      System.out.print(letter.nextElement() + "  ");

    System.out.println("\nValue at key = E : " + dict.get("E"));
    System.out.println("Value at key = R : " + dict.get("R"));

    System.out.println("Removing key E: " + dict.remove("E"));
    
    System.out.println("Is the dictionary empty? " + dict.isEmpty());

    System.out.println("Size of Dictionary : " + dict.size());
  }
}

 

Output:

Keys in Dictionary : Australia  Europe  
Values in Dictionary : A  E  
Value at key = E : Europe
Value at key = R : null
Removing key E: Europe
Is the dictionary empty? false
Size of Dictionary : 1

 

The methods hasMoreElements() and nextElement() are used to check for more constants in the enumeration and get the next value respectively. Trying to get the value of key ‘R’ returns null as it does not exist in the dictionary. Try it on java online compiler.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

What are HashTables, and how is it different from a dictionary?

HashTables are used to store key-value mappings in a hash table format. HashTables implement the Map Interface and is a direct subclass of class Dictionary. Unlike the Dictionary class, it does not provide any enumerations. Java now considers dictionaries to be obsolete. This is because Dictionary in Java is an abstract class and cannot be worked on directly.

What are iterators and enumerations?

An iterator is a cursor used to retrieve or remove elements one by one. They are universal and can be applied to any collection object. Iterator objects are created using the iterator() method present in the collection interface. Enumerations are used to get elements from legacy collections like Vectors, and HashTable.  It is a cursor used to read or retrieve elements like an iterator. However, it cannot remove elements.

Conclusion

Key-Value mappings are extremely useful for programmers to build applications. Maps, hash tables, and other related classes provide various functions to work with mapped data. The Map interface now replaces the Dictionary class but still holds good in the previous versions of the JDK. This blog discusses the Dictionary class and its features. It also lists out the different methods provided by this class for key-value mappings.

Want to master Java? Check out this amazing course.

Recommended Readings:


Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, etc. on Coding Ninjas Studio.

Also check out - Data Dictionary In Software Engineering

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Previous article
ConcurrentHashMap in Java
Next article
SortedMap
Guided path
Free
gridgp-icon
Data structures & algorithms (Beginner to Intermediate)
13 chapters
109+ Problems
gp-badge
Earn badges and level up
Live masterclass