Menu Bar

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Sunday, 13 November 2016

What is Dictionary Class In Java ?

Previously we have discussed about 

Here we learn about Dictionary Class In Java, but we must also know What is Collections Framework In Java ? and What is Collection Interface In Java ?
Class Dictionary<K,V>

Direct Known Subclasses:

Declaration of Dictionary:
public abstract class Dictionary<K,V>
extends Object

The Dictionary class is the abstract parent of any class, such as Hashtable, which maps keys to values. Every key and every value is an object. 
As a rule, the equals method should be used by implementations of this class to decide if two keys are the same.
Once the value is stored, you can retrieve it by using its key.
Although Dictionary Class is not currently deprecated, Dictionary is classified as obsolete, because it is fully superseded by Map.

Constructor Summary of Dictionary:

Constructor
Description
Dictionary()
Sole constructor.

Method Summary of Dictionary:

Modifier and Method Name
Description
abstract Enumeration<V> elements()
Returns an enumeration of the values in this dictionary.
abstract V get(Object key)
Returns the value to which the key is mapped in this dictionary.
abstract boolean isEmpty()
Tests if this dictionary maps no keys to value.
abstract Enumeration<K> keys()
Returns an enumeration of the keys in this dictionary.
abstract V put(K key, V value)
Maps the specified key to the specified value in this dictionary.
abstract V remove(Object key)
Removes the key (and its corresponding value) from this dictionary.
abstract int size()
Returns the number of entries (distinct keys) in this dictionary.

Example of Dictionary:

import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;

public class DictionaryClassExample {

public static void main(String[] args) {
Dictionary<String, Integer> dic = new Hashtable<String,Integer>();
dic.put("Pushkar", 100);
dic.put("Nitesh", 105);
dic.put("Sanjay", 110);
dic.put("Alok", 115);
System.out.println("Keys :\n------");
Enumeration key = dic.keys();
while(key.hasMoreElements()){
System.out.println(key.nextElement());
}
System.out.println("\nValues :\n--------");
Enumeration value = dic.elements();
while(value.hasMoreElements()){
System.out.println(value.nextElement());
}
}

}
Program Output:

Keys :
------
Alok
Nitesh
Sanjay
Pushkar

Values :
--------
115
105
110
100

      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Share this Blog with yours Friends !!

No comments:

Post a Comment