How to Add a Value into Java Hashtable


This example aims to add a value into Hashtable in Java.

Source Code

package com.beginner.examples;

import java.util.Hashtable;

public class SimpleHashtableExample {

	public static void main(String[] args) {
		
		//create Hashtable.
		Hashtable hashtable = new Hashtable();
		
		/*
		 * Add key value pair to Hashtable using Object put(Object key, Object value) method of Java Hashtable.
		 */
		hashtable.put("1", "Alice");
		hashtable.put("2", "Bob");
		hashtable.put("3", "Jack");
		
		Object object = hashtable.get("2");
		System.out.println(object);
	}

}

Output:

Bob

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments