Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
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-
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.
public abstract class Dictionary extends Object
Dictionary() constructor
Let us have a look at a few different methods of Dictionary Class.
size() : java.util.Dictionary.size() returns the number of key-value pairs in the Dictionary
Syntax:
public abstract int size()
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)
elements() : java.util.Dictionary.elements() returns value representation in dictionary
Syntax:
public abstract Enumeration elements()
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)
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)
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.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 14th December,2024 14th December SAT&SUN (Weekend Batch) | View Details |
edureka.co