Selasa, 15 Maret 2016

Check if a particular key exists in Java Hashtable example

1.Basic java example program to check particular element exist in hashtable
  • boolean containsKey(Object key)   This method used to check  particular key exist in hashtable

  1. package com.CheckElelemtHashtable;
  2.  
  3. import java.util.Hashtable;
  4. import java.util.Iterator;
  5.  
  6. public class HashtableExample{
  7.  
  8. public static void main(String[] args) {
  9.   
  10.  //create Hashtable object
  11.        Hashtable<String,String> hashtable = new Hashtable<String,String>();
  12.        
  13. //add key value pairs to Hashtable
  14. hashtable.put("1","Java Interview Questions");
  15. hashtable.put("2","Java Interview Programs");
  16. hashtable.put("3","Concept and exampe program");
  17. hashtable.put("4","Concept and interview Questions");
  18. hashtable.put("5","Java Quiz");
  19. hashtable.put("6","Real time examples");
  20.  
  21.  /*
  22.  To check whether a particular key exists in Hashtable we need to use
  23.  boolean containsKey(Object key) method of Hashtable class.
  24. if the Hashtable contains mapping for specified key It returns true
  25. otherwise returns false.
  26. */
  27.        
  28.  boolean isExists = hashtable.containsKey("5");
  29.  System.out.println("5 exists in Hashtable ? : " + isExists);

  30.  
  31.  
  32. Enumeration e=hashtable.elements();
  33.           
  34.  System.out.println("Display result:"); 
  35.            
  36. // display search result
  37.  while (e.hasMoreElements()) {
  38.         System.out.println(e.nextElement());
  39. }    

  40. }
  41.  
  42. }
     



Output:

  1. 5 exists in Hashtable ? : true
  2. Hashtable values : 
  3. Display result:
  4. Real time examples
  5. Java Quiz
  6. Concept and interview Questions
  7. Concept and exampe program
  8. Java Interview Programs
  9. Java Interview Questions

Bagikan

Jangan lewatkan

Check if a particular key exists in Java Hashtable example
4/ 5
Oleh

Subscribe via email

Suka dengan artikel di atas? Tambahkan email Anda untuk berlangganan.