JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: How to search a value in HashMap?


Description:

Below example shows how to find whether specified value exists or not. By using containsValue() method you can find out the value existance.


Code:
package com.java2novice.hashmap;

import java.util.HashMap;

public class MyHashMapValueSearch {
	
	public static void main(String a[]){
		HashMap<String, String> hm = new HashMap<String, String>();
		//add key-value pair to hashmap
		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 hashmap contains value SECOND INSERTED");
		} else {
			System.out.println("The hashmap does not contains value SECOND INSERTED");
		}
		if(hm.containsValue("first")){
			System.out.println("The hashmap contains value first");
		} else {
			System.out.println("The hashmap does not contains value first");
		}
	}
}

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

List Of All HashMap Sample Programs:

  1. Basic HashMap Operations.
  2. How to iterate through HashMap?
  3. How to copy Map content to another HashMap?
  4. How to search a key in HashMap?
  5. How to search a value in HashMap?
  6. How to get all keys from HashMap?
  7. How to get entry set from HashMap?
  8. How to delete all elements from HashMap?
  9. How to eliminate duplicate user defined objects as a key from HashMap?
  10. How to find user defined objects as a key from HashMap?
  11. How to delete user defined objects as a key from HashMap?
Knowledge Centre
What is adapter class?
An adapter class provides the default implementation of all methods in an event listener interface. Adapter classes are very useful when you want to process only few of the events that are handled by a particular event listener interface. You can define a new class by extending one of the adapter classes and implement only those events relevant to you.
Famous Quotations
Tomorrow is often the busiest day of the week.
-- Spanish Proverb

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.