Java/J2EE and SOA (349 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (5 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

What is Dictionary in Java and How to Create it?

Last updated on Jun 17,2021 15.7K Views

69 / 72 Blog from Java Core Concepts

Dictionary in Java is the abstract class that is the parent of any class which uses the key-value pair relationship. In this blog, we will learn more about the Dictionary class in Java and get familiar with the different methods. Below are the topics covered in this blog-

What is Dictionary in Java?

Dictionary is an abstract class representing a key/value storage repository that operates like Map. You can store the value in a Dictionary object and once it is stored, you can retrieve it by using its key.

Declaration:

public abstract class Dictionary extends Object

Constructor:

Dictionary() constructor

Methods of util.Dictionary Class 

Let us have a look at a few different methods of Dictionary Class.

Check the size of the dictionary

size() : java.util.Dictionary.size() returns the number of key-value pairs in the Dictionary

Syntax:
public abstract int size()

Add/ put values in dictionary

put(K key, V value) : java.util.Dictionary.put(K key, V value) adds key-value pair to the dictionary

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

Return values present in the dictionary

elements() : java.util.Dictionary.elements() returns value representation in dictionary

Syntax:
public abstract Enumeration elements()

Get method to fetch the values mapped with the key

get(Object key) : java.util.Dictionary.get(Object key) returns the value that is mapped with the key in the dictionary

Syntax:
public abstract V get(Object key)

Check if dictionary is empty

isEmpty() : java.util.Dictionary.isEmpty() checks whether the dictionary is empty or not.

Syntax:
public abstract boolean isEmpty()

Return true, if there is no key-value relation in the dictionary; else return false.

Removing key value from dictionary in Java

remove(Object key) : java.util.Dictionary.remove(Object key) removes the key-value pair mapped with the key.

Syntax:
public abstract V remove(Object key)

Implementation of Dictionary in Java

import java.util.*; 
public class My_Class 
{ 
    public static void main(String[] args) 
    { 
       // Initializing a Dictionary 
        Dictionary edu = new Hashtable(); 
       // put() method 
        edu.put("1000", "Edureka"); 
        edu.put("2000", "Platfrom"); 
       // elements() method : 
        for (Enumeration i = edu.elements(); i.hasMoreElements();) 
        { 
            System.out.println("Value in Dictionary : " + i.nextElement()); 
        } 
        // get() method : 
        System.out.println("nValue at key = 3000 : " + edu.get("2000")); 
        System.out.println("Value at key = 1000 : " + edu.get("2000")); 
        // isEmpty() method : 
        System.out.println("nThere is no key-value pair : " + edu.isEmpty() + "n"); 
        // keys() method : 
        for (Enumeration k = edu.keys(); k.hasMoreElements();) 
        { 
            System.out.println("Keys in Dictionary : " + k.nextElement()); 
        } 
       // remove() method : 
        System.out.println("nRemove : " + edu.remove("1000")); 
        System.out.println("Check the value of removed key : " + edu.get("1000")); 
        System.out.println("nSize of Dictionary : " + edu.size()); 
}
 } 

Output:

Value in Dictionary : Edureka
Value in Dictionary : Platform
Value at key = 3000 : null
Value at key = 1000 : Platform
There is no key-value pair : false
Keys in Dictionary : 1000
Keys in Dictionary : 2000
Remove : Edureka
Check the value of removed key : null
Size of Dictionary : 1

With this, we come to the end of this blog on the Java Dictionary Class. If you wish to learn more, check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this “Dictionary in Java” blog and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 7th December,2024

7th December

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What is Dictionary in Java and How to Create it?

edureka.co