Check if a value exists in a TreeMap in Java

To check if a value associated with a key exists in TreeMap in Java, you need to call the containsKey()  of the TreeMap.

public boolean containsValue(Object val): This method returns a true boolean if the TreeMap contains at least one instance of the value you are looking for val, otherwise false. This method is possible since Java version 1.2.

import java.util.*; 

public class Recherche_valeur_treemap {
public static void main(String[] args) {
// creating TreeMap
TreeMap< String, String> treemap = new TreeMap< String, String> ();

// insert into treemap
treemap.put("1", "a");
treemap.put("2", "b");
treemap.put("3", "c");
treemap.put("4", "d");
treemap.put("5", "e");

//display key elements and value
Set set=treemap.keySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()){
String key = ((String) iterator.next());
String val = ((String) treemap.get(key));
System.out.println(key+"-> "+val);
}

boolean exists = treemap.containsValue("a");
System.out.println("the value a exists in the list: "+exists);

exists = treemap.containsValue("f");
System.out.println("the f value exists in the list: "+exists);

}
}
Output:

1-> a
2-> b
3-> c
4-> d
5-> e
the a value exists in the list: true
the f value exists in the list: false
References:
JavaDoc: TreeMap containsValue() method