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

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (45 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (310 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

What is Dictionary in Java and How to Create it?

Published on Oct 09,2019 37 Views
69 / 72 Blog from Java Core Concepts

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

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.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.