关于集合,你搞明白了吗?

1、当向集合set中增加对象时,首先集合计算要增加对象的hashCode码,根据该值来得到一个位置来存放当前的对象,当在该位置没有一个对象存在的话,那么集合set认为该对象在集合中不存在,直接增加进去。如果在该位置有一个对象的话,接着将准备增加到集合中的对象与该位置上的对象进行equals方法比较,如果该equals方法返回false,那么集合认为集合中不存在该对象,再进行一次散列,将该对象放到散列后计算出的新地址里,如果equals方法返回true,那么集合认为集合中已经存在该对象了,不会再将该对象增加到集合中了。

2、当重写eqals方法时,必须要重写hashCode方法,如果一个类的两个对象,使用equals方法比较时,结果为true,那么该两个对象必须具有相同的hashCode

Object的hashCode()方法中有这样的说明:
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

意味着在比较对象的时候需要重写hashCode()和equals()方法,并且要保证两者返回的是相同结果,也就是说:重写了两个方法后,equals()返回的是true时,hashCode()也必须返回true;equals()返回的是false时,hashCode()也必须返回false

Object的hashCode()计算的是内存地址,只有内存地址相同时,两个Object的hashCode()方法返回相同的值。

 示例代码:

java 代码
  1. package com.test;   
  2.   
  3. import java.util.HashSet;   
  4. import java.util.Iterator;   
  5. import java.util.Set;   
  6.   
  7. public class SetTest {   
  8.   
  9.     public static void main(String[] args) {   
  10.         Set set = new HashSet();   
  11.            
  12.         set.add(new String("abc"));   
  13.         set.add(new String("xyz"));   
  14.         set.add(new String("abc"));   
  15.            
  16.         for(Iterator iter = set.iterator();iter.hasNext();)   
  17.         {   
  18.             System.out.println(iter.next());   
  19.         }   
  20.            
  21.         Set  set2 = new HashSet ();   
  22.            
  23.         set2.add(new People("zhangsan"));   
  24.         set2.add(new People("lisi"));   
  25.         set2.add(new People("zhangsan"));   
  26.            
  27.         for(Iterator  iter = set2.iterator();iter.hasNext();)   
  28.         {   
  29.             System.out.println(iter.next().getName());   
  30.         }   
  31.     }   
  32.   
  33. }   
  34.   
  35. class People   
  36. {   
  37.     private String name;   
  38.     public People(String name)   
  39.     {   
  40.         this.name = name;   
  41.     }   
  42.     public String getName()   
  43.     {   
  44.         return this.name;   
  45.     }   
  46.        
  47.     public boolean equals(Object obj)   
  48.     {   
  49.         if(null != obj)   
  50.         {   
  51.             if(this == obj)   
  52.             {   
  53.                 return true;   
  54.             }   
  55.             if(obj instanceof People)   
  56.             {   
  57.                 People people = (People)obj;   
  58.                 if((this.name).equals(people.name))   
  59.                 {   
  60.                     return true;   
  61.                 }   
  62.             }   
  63.         }   
  64.            
  65.         return false;   
  66.     }   
  67.        
  68.     public int hashCode()   
  69.     {   
  70.         return this.name.hashCode();   
  71.     }   
  72. }   

 

输出结果:

xyz
abc
zhangsan
lisi

原因是String类重写了hashCode和equals方法,使得当String对象的内容相同时,都返回true

而在程序代码People中我们也重写了Object的hashCode和equals方法,使得当People对象的name属性值内容相同时都返回true

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值