java集合框架中contains(),containsKey()和containsValue()的用法:


List集合的contains()方法用于判断集合中包不包含某个元素,返回值是boolean。 Map集合的containsKey()和containsValue()方法和上面的相同。

示例:
public static void main(String[] args) {
List list = new LinkedList();
list.add(“A”);
list.add(“B”);
list.add(“C”);
list.indexOf(“B”);
boolean flag = list.contains(“A”);
System.out.println(list.indexOf(“B”));
System.out.println(flag);

Map m = new HashMap();
m.put(“cn”, “中国”);
m.put(“us”, “美国”);
m.put(“en”, “英国”);
System.out.println(m.containsKey(“cn”));
System.out.println(m.containsValue(“英国”));
}
运行后控制台输出结果分别为:

1
true
true
true

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM