CodesJava

Easy learning with example program codes

how to search a value in hashmap in java?


HashMap

HashMap extends AbstractMap class and implements the Map interface. It contains the elements in key-value pair form. It not maintains any order for its elements. It not allowed duplicate key. A HashMap can have only one null key and multiple null values.

We can use containsValue() method to search a value in hashmap in java.

containsValue(Object v): Returns true if this map contains specified value otherwise returns false.
Syntax: public boolean containsValue(Object v)

Example

package com.codesjava;
 
import java.util.HashMap;
 
public class Test {
  public static void main(String args[]){
	//Create HashMap object.
	HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
 
	//Add objects to the HashSet.
	hashMap.put(4, "Roxy");
	hashMap.put(2, "Sunil");
	hashMap.put(5, "Sandy");
	hashMap.put(1, "Munish");
	hashMap.put(3, "Pardeep");
 
	//Print the HashMap object.
	System.out.println("HashMap elements:");
	System.out.println(hashMap);
 
	if(hashMap.containsValue("Sandy")){
            System.out.println("Hashmap contains value Sandy");
        } else {
            System.out.println("Hashmap does not contains value Sandy");
        }
  }
}

Output

HashMap elements:
{1=Munish, 2=Sunil, 3=Pardeep, 4=Roxy, 5=Sandy}
Hashmap contains value Sandy
Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki