当前位置: 首页 > >

List.contains(Object object)方法,比较对象是否相同

发布时间:

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



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






[java]?
view plain
?copy




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



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


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






[java]?
view plain
?copy




  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?????????????if?(object.equals(a[i]))?{??
  14. ????????????????return?true;??
  15. ????????????}??
  16. ????????}??
  17. ????}?else?{??
  18. ????????for?(int?i?=?0;?i?????????????if?(a[i]?==?null)?{??
  19. ????????????????return?true;??
  20. ????????????}??
  21. ????????}??
  22. ????}??
  23. ????return?false;??
  24. }??


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






[java]?
view plain
?copy




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




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


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






[java]?
view plain
?copy




  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. }??







友情链接: hackchn文档网 营销文档网 爱linux网 爱行业网 时尚网