TreeMap containsValue() method in java code example

Example 1: TreeMap containsValue() method in java

map Integer values to String keys
import java.util.TreeMap;
public class TreeMapContainsValueMethodExample
{
   public static void main(String[] args)
   {
      TreeMap<String, Integer> tm = new TreeMap<String, Integer>();
      // Map integer values to string keys
      tm.put("indigo", 16);
      tm.put("red", 12);
      tm.put("indigo", 14);
      tm.put("orange", 18);
      tm.put("violet", 20);
      System.out.println("TreeMap before using containsValue() method: " + tm);
      // Checking for the Value '12'
      System.out.println("Does value '12' present? " + tm.containsValue(12));
      // Checking for the Value '14'
      System.out.println("Does value '14' present? " + tm.containsValue(14));
      // Checking for the Value '20'
      System.out.println("Does value '20' present? " + tm.containsValue(20));
   }
}

Example 2: TreeMap containsValue() method in java

import java.util.TreeMap;
public class TreeMapContainsValueMethodExample
{
   public static void main(String[] args)
   {
      TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
      // Map string values to integer keys
      tm.put(16, "indigo");
      tm.put(12, "red");
      tm.put(14, "indigo");
      tm.put(18, "orange");
      tm.put(20, "violet");
      System.out.println("TreeMap before using containsValue() method: " + tm);
      // checking for Value 'indigo'
      System.out.println("Does value 'indigo' present? " + tm.containsValue("indigo"));
      // checking for Value 'blue'
      System.out.println("Does value 'blue' present? " + tm.containsValue("blue"));
   }
}

Tags:

Java Example