寧靜的Thinking

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

          2012年9月11日

          程序員必讀書目

               摘要: 月光博客6月12日發表了《寫給新手程序員的一封信》,翻譯自《An open letter to those who want to start programming》,我的朋友(他在本站的id是Mailper)告訴我,他希望在酷殼上看到一篇更具操作性的文章。因為他也是喜歡編程和技術的家伙,于是,我讓他把他的一些學習Python和Web編程的一些點滴總結一下。于是他給我發來了一些他的心得和經歷,我...  閱讀全文

          posted @ 2012-10-30 17:27 orangle_lzz 閱讀(285) | 評論 (0)編輯 收藏

          Dom4j使用入門—xml的解析

               摘要: 轉載:http://www.ibm.com/developerworks/cn/xml/x-dom4j.html  dom4j API 包含一個解析 XML 文檔的工具。本文中將使用這個解析器創建一個示例 XML 文檔。清單 1 顯示了這個示例 XML 文檔,catalog.xml。 清單 1. 示例 XML 文檔(catalog.xml) Code highlight...  閱讀全文

          posted @ 2012-10-18 18:25 orangle_lzz 閱讀(183) | 評論 (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 @ 2012-09-11 17:31 orangle_lzz 閱讀(202) | 評論 (0)編輯 收藏

          主站蜘蛛池模板: 焦作市| 大洼县| 雅江县| 九龙县| 阳朔县| 呼伦贝尔市| 辽源市| 凤庆县| 麟游县| 正镶白旗| 长治市| 县级市| 冷水江市| 梓潼县| 定兴县| 观塘区| 郧西县| 呼伦贝尔市| 开江县| 博兴县| 辰溪县| 锦屏县| 九寨沟县| 绵竹市| 张家界市| 道孚县| 沙河市| 许昌县| 茌平县| 广南县| 奉贤区| 南溪县| 乌鲁木齐市| 泽库县| 万全县| 苍溪县| 安国市| 鞍山市| 濮阳市| 辰溪县| 深圳市|