A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/java/util/java_util_dictionary.htm below:

Java Dictionary Class

Java Dictionary Class Introduction

The Java Dictionary class is the abstract parent of any class, such as Hashtable, which maps keys to values.Following are the important points about Dictionary −

Class declaration

Following is the declaration for java.util.Dictionary class −

public abstract class Dictionary<K,V>
   extends Object
Class constructors Sr.No. Constructor & Description 1

Dictionary()

This is the single constructor.

Class methods Methods inherited

This class inherits methods from the following classes −

Adding a Mapping to Dictionary of Integer, Integer Example

The following example shows the usage of Java Dictionary put(K,V) method. We're creating a dictionary instance using Hashtable object of Integer, Integer. Then we've added few elements to it using put(K,V) method. An enumeration is retrieved using elements() method and enumeration is then iterated to print the elements of the dictionary.

package com.tutorialspoint;

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

public class DictionaryDemo {
   public static void main(String[] args) {

      // create a new hashtable
      Dictionary<Integer, Integer> dictionary = new Hashtable<>();

      // add 2 elements
      dictionary.put(1, 1);
      dictionary.put(2, 2);

      Enumeration<Integer> enumeration = dictionary.elements();

      while(enumeration.hasMoreElements()) {
         System.out.println(enumeration.nextElement());
      }
   }
}

Let us compile and run the above program, this will produce the following result −

2
1

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4