Java Utility Library

Java Hashtable - toString() Method



The java.util.Hashtable.toString() method returns a string representation of this hashtable object in the form of a set of entries, enclosed in braces and separated ", " (comma and space).

Syntax

public String toString()

Parameters

No parameter is required.

Return Value

Returns a string representation of this hashtable.

Exception

NA.

Example:

In the example below, the java.util.Hashtable.toString() method returns a string representation of the given hashtable.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating hashtable
    Hashtable<Integer, String> Htable = new Hashtable<Integer, String>();

    //populating hashtable
    Htable.put(101, "John");
    Htable.put(102, "Marry");
    Htable.put(103, "Kim");
    Htable.put(104, "Jo"); 

    //convert the hashtable into a string
    String MyStr = Htable.toString();

    //print the string
    System.out.println(MyStr);
  }
}

The output of the above code will be:

{104=Jo, 103=Kim, 102=Marry, 101=John}

❮ Java.util - Hashtable