Java.util.EnumMap.containsValue() Method



Description

The java.util.EnumMap.containsValue(Object value) method determines if this map maps one or more keys to the specified value.

Declaration

Following is the declaration for java.util.EnumMap.containsValue() method

public boolean containsValue(Object value)

Parameters

value − the value whose presence in this map is to be tested.

Return Value

This method returns true if this map maps one or more keys to this value.

Exception

NA

Checking a Value's Presence in an EnumMap of Enum, Integer Pairs Example

The following example shows the usage of Java EnumMap containsValue() method to check if a value exists in the EnumMap instance. We've created a enum Numbers. Then EnumMap is created of enum Numbers and Integer. Few entries are added and enumMap is printed. Using containsValue() method, enumMap is checked for values and results are printed.

package com.tutorialspoint;

import java.util.EnumMap;

public class EnumMapDemo {
   
   // create an enum
   public enum Numbers{ONE, TWO, THREE, FOUR, FIVE}; 

   public static void main(String[] args) {
      
      EnumMap<Numbers,Integer> map = 
         new EnumMap<>(Numbers.class);

      // associate values in map
      map.put(Numbers.ONE, 1);
      map.put(Numbers.TWO, 2);
      map.put(Numbers.THREE,3);

      // print the whole map
      System.out.println(map); 

      // print if value is present or not
      System.out.println(map.containsValue(1));

      // print if value is present or not
      System.out.println(map.containsValue(4));
   }
}

Output

Let us compile and run the above program, this will produce the following result −

{ONE=1, TWO=2, THREE=3}
true
false

Checking a Value's Presence in an EnumMap of Enum, String Pairs Example

The following example shows the usage of Java EnumMap containsValue() method to check if a value exists in the EnumMap instance. We've created a enum Numbers. Then EnumMap is created of enum Numbers and String. Few entries are added and enumMap is printed. Using containsValue() method, enumMap is checked for values and results are printed.

package com.tutorialspoint;

import java.util.EnumMap;

public class EnumMapDemo {
   
   // create an enum
   public enum Numbers{ONE, TWO, THREE, FOUR, FIVE}; 

   public static void main(String[] args) {
      
      EnumMap<Numbers,String> map = 
         new EnumMap<>(Numbers.class);

      // associate values in map
      map.put(Numbers.ONE, "1");
      map.put(Numbers.TWO, "2");
      map.put(Numbers.THREE,"3");

      // print the whole map
      System.out.println(map); 

      // print if value is present or not
      System.out.println(map.containsValue("1"));

      // print if value is present or not
      System.out.println(map.containsValue("4")); 
   }
}

Output

Let us compile and run the above program, this will produce the following result −

{ONE=1, TWO=2, THREE=3}
true
false

Checking a Value's Presence in an EnumMap of Enum, Object Pairs Example

The following example shows the usage of Java EnumMap containsValue() method to check if a key exists in the EnumMap instance. We've created a enum Numbers. Then EnumMap is created of enum Numbers and String. Few entries are added and enumMap is printed. Using containsValue() method, enumMap is checked for keys and results are printed.

package com.tutorialspoint;

import java.util.EnumMap;

public class EnumMapDemo {
   
   // create an enum
   public enum Numbers{ONE, TWO, THREE, FOUR, FIVE}; 

   public static void main(String[] args) {
      
      EnumMap<Numbers,Student> map = 
         new EnumMap<>(Numbers.class);

      // associate values in map
      map.put(Numbers.ONE, new Student(1, "Julie"));
      map.put(Numbers.TWO, new Student(2, "Robert"));
      map.put(Numbers.THREE,new Student(3, "Adam"));

      // print the whole map
      System.out.println(map); 

      // print if value is present or not
      System.out.println(map.containsValue(new Student(1, "Julie")));

      // print if value is present or not
      System.out.println(map.containsValue(new Student(4, "Jene")));  
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   @Override
   public boolean equals(Object obj) {
      if(obj == null) return false;
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

{ONE=[ 1, Julie ], TWO=[ 2, Robert ], THREE=[ 3, Adam ]}
true
false
java_util_enummap.htm
Advertisements