Monday, March 7, 2016

Java Example : How To Create A HashTable And Iterate Its Contents

Example


package com.freesamplecode.java.hashtable;

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

public class HashTableDemo {
 public static void main(String[] args){   
  // creates an instance of hashtable
  
  Hashtable hashTable = new Hashtable<>();
  hashTable.put("nursalim", 100);
  hashTable.put("iskiyati", 150);
  hashTable.put("naura", 350);
  
  //iterate the key
  
  Enumeration keys = hashTable.keys();
  
  while(keys.hasMoreElements()){
   String key = (String) keys.nextElement();
   
   System.out.println("Key : "+key+" , Value : "+hashTable.get(key));
  }
   
 }
}

Output:


Key : nursalim , Value : 100
Key : naura , Value : 350
Key : iskiyati , Value : 150

Related Posts:

0 comments: