A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Cfan_yang 中级黑马   /  2014-12-27 20:07  /  1321 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package com.itheima;
  2. import java.io.*;
  3. import java.util.*;
  4. public class ReflectTest4 {
  5.         public static void main(String args[]) throws Exception
  6.         {
  7.                 Collection collections = new HashSet();
  8.                 ReflectPerson r1 = new ReflectPerson(3,3);
  9.                 ReflectPerson r2 = new ReflectPerson(4,4);
  10.                 ReflectPerson r3 = new ReflectPerson(5,5);
  11.                 ReflectPerson r4 = new ReflectPerson(3,3);
  12.                 collections.add(r1);
  13.                 collections.add(r2);
  14.                 collections.add(r3);
  15.                 collections.add(r4);
  16.                 System.out.println(r1.hashCode()==r4.hashCode());
  17.                 System.out.println(r1.equals(r4));
  18.                 System.out.println(collections.size());
  19.         }
  20. }
复制代码
代码如图。运行结果为:
true
true
4
为什么r1与r4的hashcode与equals都相等还存入了HashSet中。

1 个回复

倒序浏览
这个你要把ReflectPerson的equals和与之相关的内容都粘过来
这前遇到过一次在进行equals比较时更改了对象,你看看你是不是这种情况,
再有就是把两个print语句copy一份到add方法之前。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马