- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Dictionary Methods in Java
Dictionary is an abstract class that represents a key/value storage repository and operates much like Map. Given a key and value, you can store the value in a Dictionary object. Once the value is stored, you can retrieve it by using its key. Thus, like a map, a dictionary can be thought of as a list of key/value pairs.
Following are the methods defined by Dictionary are listed below −
Sr.No | Method & Description |
---|---|
1 | Enumeration elements( ) Returns an enumeration of the values contained in the dictionary. |
2 | Object get(Object key) Returns the object that contains the value associated with the key. If the key is not in the dictionary, a null object is returned. |
3 | boolean isEmpty( ) Returns true if the dictionary is empty, and returns false if it contains at least one key. |
4 | Enumeration keys( ) Returns an enumeration of the keys contained in the dictionary. |
5 | Object put(Object key, Object value) Inserts a key and its value into the dictionary. Returns null if the key is not already in the dictionary; returns the previous value associated with the key if the key is already in the dictionary. |
6 | Object remove(Object key) Removes the key and its value. Returns the value associated with the key. If the key is not in the dictionary, a null is returned. |
7 | int size( ) Returns the number of entries in the dictionary. |
Following is an example implementing put() and get() method of the Dictionary class −
Example
import java.util.*; public class Demo { public static void main(String[] args) { Dictionary dictionary = new Hashtable(); dictionary.put("20", "John"); dictionary.put("40", "Tom"); dictionary.put("60", "Steve"); dictionary.put("80", "Kevin"); dictionary.put("100", "Ryan"); dictionary.put("120", "Tim"); dictionary.put("140", "Jacob"); dictionary.put("160", "David"); System.out.println("Value at key 20 = " + dictionary.get("20")); System.out.println("Value at key 40 = " + dictionary.get("40")); System.out.println("Value at key 30 = " + dictionary.get("30")); System.out.println("Value at key 90 = " + dictionary.get("90")); } }
Output
Value at key 20 = John Value at key 40 = Tom Value at key 30 = null Value at key 90 = null
Let us see another example wherein we are displaying the Dictionary values as well using the elements () method −
Example
import java.util.*; public class Demo { public static void main(String[] args) { Dictionary dictionary = new Hashtable(); dictionary.put("20", "John"); dictionary.put("40", "Tom"); dictionary.put("60", "Steve"); dictionary.put("80", "Kevin"); dictionary.put("100", "Ryan"); dictionary.put("120", "Tim"); dictionary.put("140", "Jacob"); dictionary.put("160", "David"); System.out.println("Dictionary Values..."); for (Enumeration i = dictionary.elements(); i.hasMoreElements();) { System.out.println(i.nextElement()); } System.out.println("Value at key 20 = " + dictionary.get("20")); System.out.println("Value at key 40 = " + dictionary.get("40")); System.out.println("Value at key 30 = " + dictionary.get("30")); System.out.println("Value at key 90 = " + dictionary.get("90")); } }
Output
Dictionary Values... Tom Jacob Steve Ryan David John Kevin Tim Value at key 20 = John Value at key 40 = Tom Value at key 30 = null Value at key 90 = null
- Related Articles
- Dictionary Methods in C#
- Dictionary Methods in Python
- Dictionary Methods in Python Program
- Built-in Dictionary Functions & Methods in Python
- Dictionary Methods in Python (update(), has_key(), fromkeys()
- Dictionary Methods in Python (cmp(), len(), items()…)
- Methods in Java
- Static methods vs Instance methods in Java
- Enum Methods in Java
- Trigonometric methods in Java
- StringTokenizer methods in Java
- Default methods in Java
- What are defender methods or virtual methods in Java?
- BitSet class methods in Java
- What are methods in Java?

Advertisements