Java Utility Library

Java Dictionary - size() Method



The java.util.Dictionary.size() method returns the number of entries (distinct keys) in this dictionary.

Syntax

public abstract int size()

Parameters

No parameter is required.

Return Value

Returns the number of keys in this dictionary.

Exception

NA

Example:

In the example below, the java.util.Dictionary.size() method is used to find the number of keys in the given dictionary.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a dictionary
    Dictionary<Integer, String> Dict = 
        new Hashtable<Integer, String>();

    //populating dictionary
    Dict.put(101, "John");
    Dict.put(102, "Marry");
    Dict.put(103, "Kim");

    //printing size of the dictionary
    System.out.println("Number of keys in Dict: " + Dict.size());

    //Adding two more keys in the dictionary
    Dict.put(104, "Sam");
    Dict.put(105, "Jo");

    //printing size of the dictionary
    System.out.println("Number of keys in Dict: " + Dict.size());
  }
}

The output of the above code will be:

Number of keys in Dict: 3
Number of keys in Dict: 5

❮ Java.util - Dictionary