炒冷饭:Java中equals和HashCode的用法

2011-08-10 17:47:46 |

没有什么特色的东西,就是为了解决什么时候用equals什么时候用hashCode,简单的原则是hashCode被HashXXXX的类使用,其他的都是使用equals函数. 注意一下几点:

  • 不论Equals函数和hashCode函数自身的效率问题,从外部使用者的角度来看,hashCode速度会高于Equals。因为哈,Int总是最快的类型
  • 从通用的角度来讲,不管再安全的算法,hash总会产生重复,一个Int类型的字长决定了可变的内容的个数,而实际上一个对象的可变因素远远大于INT类型的字长,所以hash算法不是一种安全的算法——甭担心,可以满足绝大多数的应用了,除非你真能搞出两个内容不同但是哈希值相同的东西,否则还是放心使用吧。
  • 所有使用equals的容器都是用的顺序查找方法——也没有办法是用其他的办法,没法排序就没有办法使用其他的方法——我的意思是说即使你使用冒泡算法,也不见得就很优化。
    不多说了,直接上代码,自己运行,注意看每次插入的时候都干了一些啥:
    public class Bean {
        private int first =0;
        private String second ="";
        public Bean(int n,String s)
        {
            this.first = n;
            this.second =s;
        }
        public int getFirst()
        {
            return this.first;
        }
        public String getSecond()
        {
            return this.second;
        }
        
        public boolean equals(Object obj)
        {
            if(null==obj ||false==(obj instanceof Bean))
            {
                return false;
            }
            final Bean b = (Bean) obj;
            System.out.println(">> Equals!");
            return b.first ==this.first && b.getSecond().equals(this.getSecond());
        }
        public int hashCode()
        {
            int nx= super.hashCode();
            System.out.println("** HashCode: "+nx);
            return nx;
        }
        private static void testList(java.util.Collection<Bean> list){
            String testName= list.getClass().getName();
            System.out.println("--"+testName+"-----------------------------------------------------------");
            for(int n=0;n<5;n++){
                System.out.println("--"+testName+"--ROUND: "+(n+1)+" LIST SIZE: "+list.size());
                Bean b =new Bean(n,"String "+n);
                if(list.contains(b)){
                    continue;
                }else
                {
                    list.add(b);
                }
            }
            
        }
        private static void testList(java.util.Hashtable<Bean,String> list){
            String testName= list.getClass().getName();
            System.out.println("--"+testName+"-----------------------------------------------------------");
            for(int n=0;n<5;n++){
                System.out.println("--"+testName+"--ROUND: "+(n+1)+" LIST SIZE: "+list.size());
                Bean b =new Bean(n,"String "+n);
                if(list.containsKey(b)){
                    continue;
                }else
                {
                    list.put(b,">>"+n);
                }
            }
            
        }
        public static void main(String[] argvs)
        {
            testList(new java.util.ArrayList<Bean>());
            testList(new java.util.Vector<Bean>());
            testList(new java.util.ArrayDeque<Bean>());
            testList(new java.util.HashSet<Bean>());
            testList(new java.util.Hashtable<Bean,String>());
            System.out.println("All done!");
        }
    }

------------------------------------
Jeason Zhao (Mailbox/邮箱)
楚轻侯>>沈胜衣>>五斗先生>>斛律光
我知道我是谁,我默默的活着,就像空气。

Comments (1) -

FranceVoyage Marrakech :

Its same you study my opinions! You enter to understanding so plenty around this, same you wrote the e-tablet in it or something. I handle that you could do beside a some p.c. to spur the telegram house a slight portion, yet different than that, this is impressive blog. A impressive study. I’ll indeed be back

Add comment




biuquote
  • Comment
  • Preview
Loading