HashTable String toString() Method Example Program


Returns a string representation of this Hashtable object in the form of a set of entries, enclosed in braces and separated by the ASCII characters ", " (comma and space). Each entry is rendered as the key, an equals sign =, and the associated element, where the toString method is used to convert the key and element to strings.

Program

package com.candidjava;

import java.util.Hashtable;

/**
 * @author: Hari Haran
 * @discription: The toString() method is used to get a string representation of
 *               this Hashtable object in the form of a set of entries.
 */

public class HashTableToString {
	public static void main(String args[]) {
		Hashtable<Integer, String> ht1 = new Hashtable<Integer, String>();

		ht1.put(1, "Anand");
		ht1.put(2, "Hari Haran");
		ht1.put(3, "Kamal");
		ht1.put(4, "Karthikeyan");
		ht1.put(5, "Mohan");
		ht1.put(6, "Vinoth");
		ht1.put(7, "Hari Haran");

		System.out.println("String form of hash table is: " + ht1.toString());
	}
}

Output

String form of hash table is: {7=Hari Haran, 6=Vinoth, 5=Mohan, 4=Karthikeyan, 3=Kamal, 2=Hari Haran, 1=Anand}

Explanation

public String toString()
Returns a string representation of this Hashtable object in the form of a set of entries, enclosed in braces and separated by the ASCII characters ", " (comma and space). Each entry is rendered as the key, an equals sign =, and the associated element, where the toString method is used to convert the key and element to strings.
Overrides:
toString in class Object
Returns:
a string representation of this hashtable


Related Post

Comments


©candidjava.com