Java 程序检查 TreeMap 中是否存在特定值
要检查 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...
"+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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP