寧靜的Thinking

          認真工作,及時總結
          隨筆 - 3, 文章 - 0, 評論 - 0, 引用 - 0
          數據加載中……

          java中的hashcode() 函數

          這是不是說明hashcode的是什么原理,也不是說明hashcode怎么提高查詢效率。。。就是記錄一個小知識點,我們使用hashcode()方法時候的小提醒吧。
            1 import java.util.Collection;
            2 import java.util.HashSet;
            3 /**
            4  * 為什么要有==,hashcode,equels呢
            5  * hashcode可以怎么用
            6  * 使用時候容易忽視的小問題
            7  * @author lzz
            8  */
            9 public class HashCodeTest {
           10     public static void main(String[] args) {
           11         System.out.println("Test-------1---------");
           12         StringTest(); 
           13         //重寫hashcode的一個案例,有可能會有這種需求
           14         System.out.println("Test-------2--------");
           15         CollTest();
           16         System.out.println("Test-------3---------");
           17         CollTest1();
           18         //使用hashcode時候注意事項,一個小案例,可能造成內存溢出
           19         //當然使用hashcode可以提高查詢效率,這也就是set為什么用hash算法的原因吧
           20         System.out.println("Test-------4---------");
           21         CollTest2();
           22     }
           23     
           24     public static void StringTest(){    
           25         
           26     /* 這是Object中的equels
           27      *  public boolean equals(Object obj) {
           28                 return (this == obj);
           29          }*/
           30         String c=new String("a");
           31         String d=new String("a");
           32         System.out.println(c.hashCode()==d.hashCode());
           33         System.out.println(c==d);
           34         System.out.println(c.equals(d));
           35     //引用api中的解釋就是
           36     //如果根據 equals(Object) 方法,兩個對象是相等的,那么對這兩個對象中的每個對象調用 hashCode 方法都必須生成相同的整數結果
           37     //所以api建議重寫equals同時也要重寫hashcode方法    
           38     }
           39     
           40     /**
           41      * 沒有重寫hashcode方法的類在加入HashSet的情況
           42      */
           43     public static void CollTest(){
           44         Collection coll=new HashSet();
           45         CollectionTest coll1=new CollectionTest(3, 4);
           46         CollectionTest coll2=new CollectionTest(3, 5);
           47         CollectionTest coll3=new CollectionTest(3, 4);
           48         coll.add(coll1);
           49         coll.add(coll2);
           50         coll.add(coll3);
           51         System.out.println(coll.size());
           52         
           53     }
           54     
           55     /**
           56      * 重寫hashcode方法的類在加入HashSet的情況
           57      */
           58     public static void CollTest1(){
           59         Collection coll=new HashSet();
           60         CollectionTest1 coll1=new CollectionTest1(3, 4);
           61         CollectionTest1 coll2=new CollectionTest1(3, 5);
           62         CollectionTest1 coll3=new CollectionTest1(3, 4);
           63         coll.add(coll1);
           64         coll.add(coll2);
           65         coll.add(coll3);
           66         System.out.println(coll.size());
           67         
           68     }
           69     
           70     /**
           71      * 如果這樣使用會造成內存溢出問題
           72      */
           73     public static void CollTest2(){
           74         Collection coll=new HashSet();
           75         CollectionTest1 coll1=new CollectionTest1(3, 4);
           76         CollectionTest1 coll2=new CollectionTest1(3, 5);
           77         System.out.println(coll2.hashCode());
           78         coll.add(coll1);
           79         coll.add(coll2);
           80         coll2.x=4;
           81         System.out.println(coll2.hashCode());
           82         /*當coll2的hashcode生成以后加入到coll中后,我們改變從來coll2的屬性,此時它的hash已經變化了,
           83         所以當我們再去刪除它的時候,coll中存儲的coll2的hashcod已經不是原來的了
           84         所以最開始的那個coll2 就沒法被回收,  如果這樣的程序大量出現在代碼中,那么內存早晚會爆掉的*/
           85         
           86         coll.remove(coll2);
           87         System.out.println(coll.size());
           88         
           89     }
           90 }
           91 /**
           92  * 不重新寫hashcode和equels
           93  * @author lzz
           94  */
           95 class CollectionTest{
           96     int x;
           97     int y;
           98     public CollectionTest(int x,int y){
           99         this.x=x;
          100         this.y=y;
          101     }
          102 }
          103 
          104 /**
          105  * 重新寫hashcode和equels
          106  * @author lzz
          107  */
          108 class CollectionTest1{
          109     int x;
          110     int y;
          111     public CollectionTest1(int x,int y){
          112         this.x=x;
          113         this.y=y;
          114     }
          115     @Override
          116     public int hashCode() {
          117         final int prime = 31;
          118         int result = 1;
          119         result = prime * result + x;
          120         result = prime * result + y;
          121         return result;
          122     }
          123     @Override
          124     public boolean equals(Object obj) {
          125         if (this == obj)
          126             return true;
          127         if (obj == null)
          128             return false;
          129         if (getClass() != obj.getClass())
          130             return false;
          131         CollectionTest1 other = (CollectionTest1) obj;
          132         if (x != other.x)
          133             return false;
          134         if (y != other.y)
          135             return false;
          136         return true;
          137     }
          138     
          139 }
          140 

          posted on 2012-09-11 17:31 orangle_lzz 閱讀(203) 評論(0)  編輯  收藏 所屬分類: 小知識積累


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 上犹县| 天门市| 定兴县| 从江县| 阳谷县| 池州市| 榆树市| 镇康县| 衡水市| 新疆| 建始县| 怀仁县| 吉安市| 象州县| 渭源县| 房山区| 淮北市| 阳山县| 平凉市| 西安市| 汉中市| 油尖旺区| 沛县| 营口市| 右玉县| 乐东| 娱乐| 阜康市| 德保县| 婺源县| 巢湖市| 察哈| 仁怀市| 辽中县| 旬邑县| 石狮市| 河池市| 称多县| 扎赉特旗| 阳西县| 旅游|