beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Check key & Value existence in Hashtable example – Java

By Chaitanya Singh | Filed Under: Java Collections

Example

In this example we are gonna see how to check Key and value existence in Hashtable. We will be using following two methods to perform this check:

containsKey(Object key): To check if the key present in Hashtable.
containsvalue(Object value): To check if the value present in Hashtable.

import java.util.Hashtable;

public class CheckKeyValueExample {
 
 public static void main(String[] args) {
 
   // Creating a Hashtable instance
   Hashtable<String, String> hashtable = new Hashtable<String, String>();
 
   // Adding key-value pairs to Hashtable
   hashtable.put("A", "Apple");
   hashtable.put("B", "Orange");
   hashtable.put("C", "Mango");
   hashtable.put("D", "Banana");
   hashtable.put("E", "Grapes");
 
   /* Checking Key existence using containsKey() method.
    * boolean containsKey(Object key) method returns true if the 
    * Hashtable contains the key, otherwise it returns false.
    */
   boolean keyFlag1 = hashtable.containsKey("A");
   System.out.println("Key A exists in Hashtable?: " + keyFlag1);
   boolean keyFlag2 = hashtable.containsKey("D");
   System.out.println("Key D exists in Hashtable?: " + keyFlag2);
   boolean keyFlag3 = hashtable.containsKey("G");
   System.out.println("Key G exists in Hashtable?: " + keyFlag3);
 
   /* Checking Value existence using containsValue() method.
    * boolean containsKey(Object key) method Returns true 
    * if this Hashtable maps one or more keys to this value..
    */
   boolean vFlag1 = hashtable.containsValue("Orange");
   System.out.println("Value Orange exists in Hashtable?: "+vFlag1);
   boolean vFlag2 = hashtable.containsValue("XYZ");
   System.out.println("Value XYZ exists in Hashtable?: "+vFlag2);
   boolean vFlag3 = hashtable.containsValue("Apple");
   System.out.println("Value Apple exists in Hashtable?: "+vFlag3);
 }
}

Output:

Key A exists in Hashtable?: true
Key D exists in Hashtable?: true
Key G exists in Hashtable?: false
Value Orange exists in Hashtable?: true
Value XYZ exists in Hashtable?: false
Value Apple exists in Hashtable?: true

Enjoyed this post? Try these related posts

  1. Remove Key-value mapping from TreeMap example
  2. Java ArrayList remove(Object obj) Method example
  3. How to remove Vector elements using index in java example
  4. How to get the last element of Arraylist?
  5. How to get sub list of Vector example in java
  6. Java – Check if a particular value exists in HashMap example

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

  • Java Tutorial
  • OOPs Concepts

Java Collections

  • ArrayList
  • LinkedList
  • ArrayList vs LinkedList
  • Vector
  • ArrayList vs Vector
  • HashMap
  • TreeMap
  • LinkedHashMap
  • HashSet
  • TreeSet
  • LinkedHashSet
  • Hashtable
  • HashMap vs Hashtable
  • Queue
  • PriorityQueue
  • Deque & ArrayDeque
  • Iterator
  • ListIterator
  • Comparable Interface
  • Comparator Interface
  • Java Collections Interview Q

MORE ...

  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap