key-value pair mapped in the dictionary.
2.
get()
The get() method takes the key as the argument and returns the value that is mapped to it. If no value is mapped with the given key, it simply returns null.
Syntax: public abstract V get(Object key)
Parameters: key – key whose mapped value we want
Return: value mapped with the argument key.
3.
elements
()
The elements() method is used to represent all the values present inside the Dictionary. It is usually used with loop statements as they can then represent one value at a time.
Syntax: public abstract Enumeration elements()
Return: value enumeration in the dictionary.
4.
keys
()
As the elements() method returns the enumerated values present inside the dictionary; similarly, the keys() method returns the enumerated keys present inside the dictionary.
Syntax: public abstract Enumeration keys()
Return: The key enumeration in the dictionary.
5.
isEmpty()
The isEmpty() method returns a boolean value, which is true if there are no key-value pairs present inside the Dictionary. If even any single key-value pair resides inside the dictionary, it returns false.
Syntax: public abstract boolean isEmpty()
Return: It returns true if there is no key-value relation in the dictionary; else false.
6.
remove(key
)
The remove() method takes the key as its argument, and it simply removes both the key and the value mapped with it from the dictionary.
Syntax: public abstract V remove(Object key)
Parameters: key: a key to be removed
Return: The key enumeration in the dictionary.
7.
size()
The size() method returns the total number of key-value pairs present inside the Dictionary.
Syntax: public abstract int size()
Return: It returns the no. of key-value pairs in the Dictionary.
The following program code is an example of using Dictionaries in Java.
import java.util.Dictionary; import java.util.Enumeration; import java.util.Hashtable; public class Dict { public static void main(String[] args) { Dictionary dictionary = new Hashtable(); dictionary.put("Apple", "A fruit"); dictionary.put("Ball", "A round shaped toy"); dictionary.put("Car", "A four wheeler vehicle designed to accomodate usually four people"); dictionary.put("Dog", "An animal with four legs and one tail"); System.out.println("nApple: " + dictionary.get("Apple")); System.out.println("Dog: " + dictionary.get("Dog")); System.out.println("Elephant: " + dictionary.get("Elephant")); System.out.println(); for (Enumeration i = dictionary.elements(); i.hasMoreElements();) { System.out.println("Values contained in Dictionary : " + i.nextElement()); } System.out.println(); for (Enumeration k = dictionary.keys(); k.hasMoreElements();) { System.out.println("Keys contianed in Dictionary : " + k.nextElement()); } System.out.println("nThe dictionary is empty? " + dictionary.isEmpty()); dictionary.remove("Dog"); System.out.println("nDog: " + dictionary.get("Dog")); System.out.println("nSize of Dictionary : " + dictionary.size()); } }See the output.
Finally, Java Dictionary Class Example Tutorial is over.
Recommended Posts
Nested class In Java Example
Java Type Casting Example
Java Interface Example
HashMap in Java Example
Java ArrayList Example
Java Scanner Class Example
Java Constructor Example