若要检查TreeMap中是否存在特定值,请使用containsValue()
方法。
首先创建一个TreeMap并添加一些元素-
TreeMap<Integer,String> m = new TreeMap<Integer,String>(); m.put(1,"PHP"); m.put(2,"jQuery"); m.put(3,"JavaScript"); m.put(4,"Ruby"); m.put(5,"Java"); m.put(6,"AngularJS"); m.put(7,"ExpressJS");
现在,我们需要检查“ Java”值是否存在。为此,请使用如下containsValue()
方法-
m.containsValue("Java")
以下是检查TreeMap中是否存在特定值的示例-
import java.util.*; public class Demo { public static void main(String args[]) { TreeMap<Integer,String> m = new TreeMap<Integer,String>(); m.put(1,"PHP"); m.put(2,"jQuery"); m.put(3,"JavaScript"); m.put(4,"Ruby"); m.put(5,"Java"); m.put(6,"AngularJS"); m.put(7,"ExpressJS"); System.out.println("TreeMap Elements...\n"+m); System.out.println("Does value PHP exist in the TreeMap = "+m.containsValue("Java")); } }
输出结果
TreeMap Elements... {1=PHP, 2=jQuery, 3=JavaScript, 4=Ruby, 5=Java, 6=AngularJS, 7=ExpressJS} Does value PHP exist in the TreeMap = true