1.Basic java example program to check particular element exist in hashtable
Output:
- boolean containsKey(Object key) This method used to check particular key exist in hashtable
- package com.CheckElelemtHashtable;
- import java.util.Hashtable;
- import java.util.Iterator;
- public class HashtableExample{
- public static void main(String[] args) {
- //create Hashtable object
- Hashtable<String,String> hashtable = new Hashtable<String,String>();
- //add key value pairs to Hashtable
- hashtable.put("1","Java Interview Questions");
- hashtable.put("2","Java Interview Programs");
- hashtable.put("3","Concept and exampe program");
- hashtable.put("4","Concept and interview Questions");
- hashtable.put("5","Java Quiz");
- hashtable.put("6","Real time examples");
- /*
- To check whether a particular key exists in Hashtable we need to use
- boolean containsKey(Object key) method of Hashtable class.
- if the Hashtable contains mapping for specified key It returns true
- otherwise returns false.
- */
- boolean isExists = hashtable.containsKey("5");
- System.out.println("5 exists in Hashtable ? : " + isExists);
- Enumeration e=hashtable.elements();
- System.out.println("Display result:");
- // display search result
- while (e.hasMoreElements()) {
- System.out.println(e.nextElement());
- }
- }
- }
Output:
- 5 exists in Hashtable ? : true
- Hashtable values :
- Display result:
- Real time examples
- Java Quiz
- Concept and interview Questions
- Concept and exampe program
- Java Interview Programs
- Java Interview Questions