一、用法:list集合中contains() 用于判断集合中 是否 包含指定的元素。list会将括号内的元素和list中存在的元素进行逐个比对,若有相等的,返回结果为true,若没有则返回结果为false。
二、举例说明:用下方代码验证:
public static void main(string[] args) { list newlist = new arraylist();//创建一个空数组 newlist.add("name"); newlist.add("age"); newlist.add("sex"); newlist.add("birth");//往数组中加一些元素 boolean res = false; if(newlist.contains("birthday")){ res=true; log.info("包含,返回"+res); }else { log.info("不包含,返回"+res); } }
测试newlist数组中是否包含元素“birthday”
测试newlist数组中是否包含元素“birth”
三、拓展string类中的contains()方法:当且仅当此字符串包含指定的 char 值序列,即判断指定内容中是否包含括号中的内容。
举例说明:
public static void main(string[] args) { string str="csdn程序媛"; boolean res = false; if(str.contains("程序媛")){ res=true; log.info("包含程序媛,返回"+res); }else { log.info("不包含程序媛,返回"+res); }
测试string类型“csdn程序媛”是否包含“程序媛”
如果string类型的字符串中包含字母时,需要注意区分大小写
测试string类型“csdn程序媛”是否包含小写“csdn”
补充:java中list.contains()方法比较的是地址而不是值使用list.contains(object object)方法判断arraylist是否包含一个元素对象(针对于对象的属性值相同,但对象地址不同的情况),如果没有重写list<e>的元素对象object中的equals方法,默认如下:
使用list.contains(object object)方法判断arraylist是否包含一个元素对象(针对于对象的属性值相同,但对象地址不同的情况),如果没有重写list<e>的元素对象object中的equals方法,默认如下:
@override public boolean equals(object o) { // todo auto-generated method stub return super.equals(o); }
将导致contains方法始终返回false。
查看arraylist的contains方法的源码如下:
/** * searches this {@code arraylist} for the specified object. * * @param object * the object to search for. * @return {@code true} if {@code object} is an element of this * {@code arraylist}, {@code false} otherwise */ @override public boolean contains(object object) { object[] a = array; int s = size; if (object != null) { for (int i = 0; i < s; i++) { if (object.equals(a[i])) { return true; } } } else { for (int i = 0; i < s; i++) { if (a[i] == null) { return true; } } } return false; }
可以看出,contains方法依据object的equals方法来判断是否包含某一元素,继续查看object类中的equals方法,源码如下:
public boolean equals(object o) { return this == o; }
所以,使用“==”比较对象的地址,如果是同一对象即地址相同的情况下,才会返回true,而对于对象属性值相同但地址不同的不同对象,始终返回false!
如果需要依据对象属性值是否相同来判断arraylist是否包含某一对象,则需要重写object的equals方法,并在equals方法中一一比较对象的每个属性值,如:
package com.feng.lejuan.entity; public class questioninfo { private string questionid; private string answerid; private string subquestionid; private string result; public questioninfo() { super(); } public questioninfo(string questionid, string answerid, string subquestionid, string result) { super(); this.questionid = questionid; this.answerid = answerid; this.subquestionid = subquestionid; this.result = result; } public string getquestionid() { return questionid; } public void setquestionid(string questionid) { this.questionid = questionid; } public string getanswerid() { return answerid; } public void setanswerid(string answerid) { this.answerid = answerid; } public string getsubquestionid() { return subquestionid; } public void setsubquestionid(string subquestionid) { this.subquestionid = subquestionid; } public string getresult() { return result; } public void setresult(string result) { this.result = result; } @override public boolean equals(object o) { if (o instanceof questioninfo) { questioninfo question = (questioninfo) o; return this.questionid.equals(question.questionid) && this.subquestionid.equals(question.subquestionid) && this.answerid.equals(question.answerid) && this.result.equals(question.result); } return super.equals(o); } @override public string tostring() { return "questioninfo [questionid=" + questionid + ", answerid=" + answerid + ", subquestionid=" + subquestionid + ", result=" + result + "]"; } }
以上就是java中list.contains()怎么使用的详细内容。