What is Dictionary class

What is Dictionary class

Dictionary is abstract class and it represents key/value storage repository. It is same as Map where . By supplying a key and value, we can store the value in a Dictionary. Once value is stored we can retrieve it by using key. Although it is not currently deprecated, Dictionary classified as obsolete, because it is fully superseded by Map.

However, Dictionary could still be used and inJDK 5, Dictionary was made generic. It can be declared as below:

//where K = key and V = value
Dictionary<K, V>

 

Please see java example of dictionary below:

package com.javahonk.dictionarytest;

import java.util.*;

public class DictionaryTest {

	public static void main(String[] args) {

	Dictionary<String, String> dictionary = 
			new Hashtable<String, String>();
	dictionary.put("Name", "Java Honk");
	dictionary.put("Test", "Dictionary");

	Enumeration<String> enumeration = dictionary.keys();

	while (enumeration.hasMoreElements()) {
		String string = (String) enumeration.nextElement();
		System.out.println(dictionary.get(string));

	}

   }

}

 

Output:

dictionary

 

Check out all interview questions

Leave a Reply

Your email address will not be published. Required fields are marked *