Tuesday, 3 June 2014

HashMap : containsValue (Object value) : Search for a value in the Map

boolean containsValue(Object value)
Returns true if this map maps one or more keys to the specified value.

import java.util.*;

class HashMapContainsValue{
  public static void main(String args[]){
    HashMap<Integer, String> myMap1 = new HashMap<> ();
  
  /* Add data to the myMap1 */
  myMap1.put(1, "First");
  myMap1.put(2, "Second");
  myMap1.put(3, "Third");
  myMap1.put(4, "Fourth");
  
  System.out.println("Data in myMap1 is");
  System.out.println(myMap1);
  
  System.out.print("\nIs myMap1 contains Value First ");
  System.out.println(myMap1.containsValue("First"));
  
  System.out.print("Is myMap1 contains Value Last ");
  System.out.println(myMap1.containsValue("Last"));
  }
}

Output
Data in myMap1 is
{1=First, 2=Second, 3=Third, 4=Fourth}

Is myMap1 contains Value First true
Is myMap1 contains Value Last false


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment