使用List.contains(Object object)方法判断ArrayList是否包含一个元素对象(针对于对象的属性值相同,但对象地址不同的情况),如果没有重写List<E>的元素对象Object中的equals方法,默认如下:
1 2 3 4 5 |
@Override public boolean equals(Object o) { // TODO Auto-generated method stub return super .equals(o); } |
将导致contains方法始终返回false。
查看ArrayList的contains方法的源码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
/** * 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方法,源码如下:
1 2 3 |
public boolean equals(Object o) { return this == o; } |
所以,使用[==]比较对象的地址,如果是同一对象即地址相同的情况下,才会返回true,而对于对象属性值相同但地址不同的不同对象,始终返回false!
如果需要依据对象属性值是否相同来判断ArrayList是否包含某一对象,则需要重写Object的equals方法,并在equals方法中一一比较对象的每个属性值,如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
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(Object object)方法的文章就介绍到这了,更多相关Java List.contains(Object object)内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
原文链接:https://blog.csdn.net/growing_tree/article/details/46622579
查看更多关于Java中List.contains(Object object)方法使用的详细内容...