zoukankan      html  css  js  c++  java
  • List.contains(Object object)方法,比较对象是否相同

    原文地址:https://blog.csdn.net/growing_tree/article/details/46622579

    使用List.contains(Object object)方法判断ArrayList是否包含一个元素对象(针对于对象的属性值相同,但对象地址不同的情况),如果没有重写List<E>的元素对象Object中的equals方法,默认如下:

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


    将导致contains方法始终返回false。

    查看ArrayList的contains方法的源码如下:

    1. /** 
    2.  * Searches this {@code ArrayList} for the specified object. 
    3.  * 
    4.  * @param object 
    5.  *            the object to search for. 
    6.  * @return {@code true} if {@code object} is an element of this 
    7.  *         {@code ArrayList}, {@code false} otherwise 
    8.  */  
    9. @Override public boolean contains(Object object) {  
    10.     Object[] a = array;  
    11.     int s = size;  
    12.     if (object != null) {  
    13.         for (int i = 0; i < s; i++) {  
    14.             if (object.equals(a[i])) {  
    15.                 return true;  
    16.             }  
    17.         }  
    18.     } else {  
    19.         for (int i = 0; i < s; i++) {  
    20.             if (a[i] == null) {  
    21.                 return true;  
    22.             }  
    23.         }  
    24.     }  
    25.     return false;  
    26. }  

    可以看出,contains方法依据Object的equals方法来判断是否包含某一元素,继续查看Object类中的equals方法,源码如下:

    1. public boolean equals(Object o) {  
    2.     return this == o;  
    3. }  


    所以,使用“==”比较对象的地址,如果是同一对象即地址相同的情况下,才会返回true,而对于对象属性值相同但地址不同的不同对象,始终返回false!

    如果需要依据对象属性值是否相同来判断ArrayList是否包含某一对象,则需要重写Object的equals方法,并在equals方法中一一比较对象的每个属性值,如:



      1. package com.feng.lejuan.entity;  
      2.   
      3. public class QuestionInfo {  
      4.   
      5.     private String questionId;  
      6.       
      7.     private String answerId;  
      8.       
      9.     private String subQuestionId;  
      10.       
      11.     private String result;  
      12.   
      13.     public QuestionInfo() {  
      14.         super();  
      15.           
      16.     }  
      17.   
      18.     public QuestionInfo(String questionId, String answerId,  
      19.             String subQuestionId, String result) {  
      20.         super();  
      21.         this.questionId = questionId;  
      22.         this.answerId = answerId;  
      23.         this.subQuestionId = subQuestionId;  
      24.         this.result = result;  
      25.     }  
      26.   
      27.     public String getQuestionId() {  
      28.         return questionId;  
      29.     }  
      30.   
      31.     public void setQuestionId(String questionId) {  
      32.         this.questionId = questionId;  
      33.     }  
      34.   
      35.     public String getAnswerId() {  
      36.         return answerId;  
      37.     }  
      38.   
      39.     public void setAnswerId(String answerId) {  
      40.         this.answerId = answerId;  
      41.     }  
      42.   
      43.     public String getSubQuestionId() {  
      44.         return subQuestionId;  
      45.     }  
      46.   
      47.     public void setSubQuestionId(String subQuestionId) {  
      48.         this.subQuestionId = subQuestionId;  
      49.     }  
      50.   
      51.     public String getResult() {  
      52.         return result;  
      53.     }  
      54.   
      55.     public void setResult(String result) {  
      56.         this.result = result;  
      57.     }  
      58.   
      59.     @Override  
      60.     public boolean equals(Object o) {  
      61.         if (o instanceof QuestionInfo) {  
      62.             QuestionInfo question = (QuestionInfo) o;  
      63.             return this.questionId.equals(question.questionId)  
      64.                     && this.subQuestionId.equals(question.subQuestionId)  
      65.                     && this.answerId.equals(question.answerId)  
      66.                     && this.result.equals(question.result);  
      67.         }  
      68.         return super.equals(o);  
      69.     }  
      70.       
      71.     @Override  
      72.     public String toString() {  
      73.         return "QuestionInfo [questionId=" + questionId + ", answerId="  
      74.                 + answerId + ", subQuestionId=" + subQuestionId + ", result="  
      75.                 + result + "]";  
      76.     }  
      77.       
      78. }  
  • 相关阅读:
    Win10 UWP Tile Generator
    Win10 BackgroundTask
    UWP Tiles
    UWP Ad
    Win10 build package error collections
    Win10 八步打通 Nuget 发布打包
    Win10 UI入门 pivot multiable DataTemplate
    Win10 UI入门 导航滑动条 求UWP工作
    UWP Control Toolkit Collections 求UWP工作
    Win10 UI入门 SliderRectangle
  • 原文地址:https://www.cnblogs.com/xxl910/p/12749604.html
Copyright © 2011-2022 走看看