JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: How to search a value in Hashtable?


Description:

Below example shows how to search a value from the Hashtable. The method containsValue() helps us to find whether the specified value exists or not.


Code:
package com.java2novice.hashtable;

import java.util.Hashtable;

public class MyHashtableValueSearch {

	public static void main(String a[]){
		Hashtable<String, String> hm = new Hashtable<String, String>();
		//add key-value pair to Hashtable
		hm.put("first", "FIRST INSERTED");
		hm.put("second", "SECOND INSERTED");
		hm.put("third","THIRD INSERTED");
		System.out.println(hm);
		if(hm.containsValue("SECOND INSERTED")){
			System.out.println("The Hashtable contains value SECOND INSERTED");
		} else {
			System.out.println("The Hashtable does not contains value SECOND INSERTED");
		}
		if(hm.containsValue("first")){
			System.out.println("The Hashtable contains value first");
		} else {
			System.out.println("The Hashtable does not contains value first");
		}
	}
}

Output:
{third=THIRD INSERTED, second=SECOND INSERTED, first=FIRST INSERTED}
The Hashtable contains value SECOND INSERTED
The Hashtable does not contains value first
<< Previous Program | Next Program >>

List Of All Hashtable Sample Programs:

  1. Basic Hashtable Operations.
  2. How to iterate through Hashtable?
  3. How to copy Map content to another Hashtable?
  4. How to search a key in Hashtable?
  5. How to search a value in Hashtable?
  6. How to get all keys from Hashtable?
  7. How to get entry set from Hashtable?
  8. How to delete all elements from Hashtable?
  9. How to read elements using Enumeration in Hashtable?
  10. Hashtable implementation with equals and hashcode example.
  11. How to fetch values with user define object as keys in Hashtable?
  12. How to eliminate duplicate keys (user defined objects) with Hashtable?
Knowledge Centre
Difference between Enumeration and Iterator
The functionality of Enumeration and the Iterator are same. You can get remove() from Iterator to remove an element, while while Enumeration does not have remove() method. Using Enumeration you can only traverse and fetch the objects, where as using Iterator we can also add and remove the objects. So Iterator can be useful if you want to manipulate the list and Enumeration is for read-only access.
Famous Quotations
It is easier to fight for one’s principles than to live up to them.
-- Alfred Adler

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.