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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© bowen-xiao 中级黑马   /  2015-1-18 13:33  /  1543 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. public void collectionTest() {
  2. //                 ArrayList是一个有顺序的集合,相当于一个数组,里面有重复的对象,存放的是对象的引用
  3. //                 Collection colls = new ArrayList();
  4.                
  5. //                 HashSet放入一个对象时与原来所有的对象进行比较,
  6. //                如果一样就不放入集合,例如前面有十个都要进行比较
  7.                 Collection colls = new HashSet();

  8. //                 HashCode值是内存地址进行换算出来的一个值,
  9. //                首先判断要放入对象的hashcode值与集合中的任意一个元素的hashcode值是否相等,
  10. //                如果不相等直接将该对象放入集合中。
  11. //                如果hashcode值相等,然后再通过equals方法判断要放入对象与集合中的任意一个对象是否相等,
  12. //                如果equals判断不相等,直接将该元素放入到集合中,否则不放入。
  13.                 demo3 d4 = new demo3(5, 5);
  14.                 demo3 d5 = new demo3(4, 4);
  15.                 demo3 d6 = new demo3(3, 3);
  16.                 demo3 d7 = new demo3(3, 3);
  17. //                 判断d7 是否与 d6为相同对象时需要HashCode和equals都返回为true时才能确定
  18.                
  19. //                 equals方法主要用于判断对象的内存地址引用是不是同一个地址(是不是相同值的对象)
  20. //                 如colls.add(d6)添加两次,第二次是添加不进去的
  21.                 colls.add(d6);
  22.                 colls.add(d5);
  23.                 colls.add(d4);
  24.                 colls.add(d7);
  25.                 colls.add(d6);

  26. //                 d4值的发生改变后,是不能被remove掉的,这样会发生泄漏的,
  27. //                 系统在长时间运行后,会内存耗尽最后崩溃。
  28.                 d4.y = 6;
  29.                 colls.remove(d4);

  30.                 System.out.println(colls.size());
  31.         }

  32.          class demo3{
  33.                 private int x ;
  34.                 private int y;
  35.                
  36.                 public demo3(int x, int y) {
  37.                         super();
  38.                         this.x = x;
  39.                         this.y = y;
  40.                 }
  41.                 public int getX() {
  42.                         return x;
  43.                 }
  44.                 public void setX(int x) {
  45.                         this.x = x;
  46.                 }
  47.                 public int getY() {
  48.                         return y;
  49.                 }
  50.                 public void setY(int y) {
  51.                         this.y = y;
  52.                 }
  53.                 @Override
  54.                 public int hashCode() {
  55.                         final int prime = 31;
  56.                         int result = 1;
  57.                         result = prime * result + getOuterType().hashCode();
  58.                         result = prime * result + x;
  59.                         result = prime * result + y;
  60.                         return result;
  61.                 }
  62.                 @Override
  63.                 public boolean equals(Object obj) {
  64.                         if (this == obj)
  65.                                 return true;
  66.                         if (obj == null)
  67.                                 return false;
  68.                         if (getClass() != obj.getClass())
  69.                                 return false;
  70.                         demo3 other = (demo3) obj;
  71.                         if (!getOuterType().equals(other.getOuterType()))
  72.                                 return false;
  73.                         if (x != other.x)
  74.                                 return false;
  75.                         if (y != other.y)
  76.                                 return false;
  77.                         return true;
  78.                 }
  79.                 private ReflectTest getOuterType() {
  80.                         return ReflectTest.this;
  81.                 }
  82.                
  83.         }
复制代码


4 个回复

倒序浏览
;P恭喜啊,反射完事了,你什么时候走流程?
回复 使用道具 举报
云兮丶 发表于 2015-1-18 17:11
恭喜啊,反射完事了,你什么时候走流程?

边走边看,最近在走;哥们你呢?
回复 使用道具 举报
bowen-xiao 发表于 2015-1-18 20:27
边走边看,最近在走;哥们你呢?

恩 准备最近开始走了
回复 使用道具 举报
云兮丶 发表于 2015-1-18 22:36
恩 准备最近开始走了

打算报哪一期呀!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马