溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java中List.contains(Object?object)方法怎么使用

發(fā)布時間:2022-04-08 13:44:39 來源:億速云 閱讀:237 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“Java中List.contains(Object object)方法怎么使用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Java中List.contains(Object object)方法怎么使用”吧!

使用List.contains(Object object)方法判斷ArrayList是否包含一個元素對象(針對于對象的屬性值相同,但對象地址不同的情況),如果沒有重寫List<E>的元素對象Object中的equals方法,默認(rèn)如下:

    @Override
    public boolean equals(Object o) {
        // TODO Auto-generated method stub
        return super.equals(o);
    }

將導(dǎ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方法依據(jù)Object的equals方法來判斷是否包含某一元素,繼續(xù)查看Object類中的equals方法,源碼如下:

    public boolean equals(Object o) {
        return this == o;
    }

所以,使用“==”比較對象的地址,如果是同一對象即地址相同的情況下,才會返回true,而對于對象屬性值相同但地址不同的不同對象,始終返回false!

如果需要依據(jù)對象屬性值是否相同來判斷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(Object object)方法怎么使用”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI