Java中list.contains()的用法及拓展
作者:雯雯是程序媛呀 时间:2022-11-06 21:04:59
一、用法:
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 + "]";
}
}
来源:https://blog.csdn.net/qq_48461195/article/details/123126259
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android实现图像灰度化、线性灰度变化和二值化处理方法
SpringBoot中实现接收文件和对象
![](https://img.aspxhome.com/file/2023/0/79280_0s.png)
SpringBoot集成gRPC微服务工程搭建实践的方法
java增强for循环的实现方法
Android实现跳动的小球加载动画效果
![](https://img.aspxhome.com/file/2023/5/95015_0s.gif)
Object类toString()和equals()方法使用解析
C# 参数按照ASCII码从小到大排序(字典序)
Gradle属性设置及环境变量全面教程
Mybatis分页插件PageHelper的配置和简单使用方法(推荐)
SpringBoot使用Kaptcha实现验证码的生成与验证功能
![](https://img.aspxhome.com/file/2023/7/66977_0s.png)
springdata jpa使用Example快速实现动态查询功能
Android中监听未接来电的2种方法
SpringCloud之Feign示例详解
IDEA 2020.2 +Gradle 6.6.1 + Spring Boot 2.3.4 创建多模块项目的超详细教程
![](https://img.aspxhome.com/file/2023/8/121928_0s.png)
java 线程中start方法与run方法的区别详细介绍
Springboot通过lucene实现全文检索详解流程
![](https://img.aspxhome.com/file/2023/5/84695_0s.png)
Qt之调用C#的动态库的解决方法
![](https://img.aspxhome.com/file/2023/1/67061_0s.png)
Java JVM中线程状态详解
![](https://img.aspxhome.com/file/2023/9/75759_0s.jpg)
深入浅析C#泛型类型
![](https://img.aspxhome.com/file/2023/9/111139_0s.png)
C# 设置Chart的X轴为时间轴详情
![](https://img.aspxhome.com/file/2023/2/119512_0s.png)