寧?kù)o的Thinking

          認(rèn)真工作,及時(shí)總結(jié)
          隨筆 - 3, 文章 - 0, 評(píng)論 - 0, 引用 - 0
          數(shù)據(jù)加載中……

          2012年10月30日

          程序員必讀書(shū)目

               摘要: 月光博客6月12日發(fā)表了《寫給新手程序員的一封信》,翻譯自《An open letter to those who want to start programming》,我的朋友(他在本站的id是Mailper)告訴我,他希望在酷殼上看到一篇更具操作性的文章。因?yàn)樗彩窍矚g編程和技術(shù)的家伙,于是,我讓他把他的一些學(xué)習(xí)Python和Web編程的一些點(diǎn)滴總結(jié)一下。于是他給我發(fā)來(lái)了一些他的心得和經(jīng)歷,我...  閱讀全文

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

          2012年10月18日

          Dom4j使用入門—xml的解析

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

          posted @ 2012-10-18 18:25 orangle_lzz 閱讀(183) | 評(píng)論 (0)編輯 收藏

          2012年9月11日

          java中的hashcode() 函數(shù)

          這是不是說(shuō)明hashcode的是什么原理,也不是說(shuō)明hashcode怎么提高查詢效率。。。就是記錄一個(gè)小知識(shí)點(diǎn),我們使用hashcode()方法時(shí)候的小提醒吧。
            1 import java.util.Collection;
            2 import java.util.HashSet;
            3 /**
            4  * 為什么要有==,hashcode,equels呢
            5  * hashcode可以怎么用
            6  * 使用時(shí)候容易忽視的小問(wèn)題
            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的一個(gè)案例,有可能會(huì)有這種需求
           14         System.out.println("Test-------2--------");
           15         CollTest();
           16         System.out.println("Test-------3---------");
           17         CollTest1();
           18         //使用hashcode時(shí)候注意事項(xiàng),一個(gè)小案例,可能造成內(nèi)存溢出
           19         //當(dāng)然使用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     //如果根據(jù) equals(Object) 方法,兩個(gè)對(duì)象是相等的,那么對(duì)這兩個(gè)對(duì)象中的每個(gè)對(duì)象調(diào)用 hashCode 方法都必須生成相同的整數(shù)結(jié)果
           37     //所以api建議重寫equals同時(shí)也要重寫hashcode方法    
           38     }
           39     
           40     /**
           41      * 沒(méi)有重寫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      * 如果這樣使用會(huì)造成內(nèi)存溢出問(wèn)題
           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         /*當(dāng)coll2的hashcode生成以后加入到coll中后,我們改變從來(lái)coll2的屬性,此時(shí)它的hash已經(jīng)變化了,
           83         所以當(dāng)我們?cè)偃h除它的時(shí)候,coll中存儲(chǔ)的coll2的hashcod已經(jīng)不是原來(lái)的了
           84         所以最開(kāi)始的那個(gè)coll2 就沒(méi)法被回收,  如果這樣的程序大量出現(xiàn)在代碼中,那么內(nèi)存早晚會(huì)爆掉的*/
           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) | 評(píng)論 (0)編輯 收藏

          主站蜘蛛池模板: 大姚县| 锦州市| 环江| 富宁县| 高唐县| 西充县| 万荣县| 满洲里市| 永靖县| 西丰县| 安阳县| 太康县| 唐河县| 东海县| 峨边| 木里| 安龙县| 泰兴市| 桐梓县| 抚州市| 焉耆| 峨眉山市| 临朐县| 饶河县| 白玉县| 万荣县| 浦县| 富川| 苍溪县| 永寿县| 积石山| 景德镇市| 崇州市| 白朗县| 万全县| 土默特左旗| 岳西县| 吉安市| 浦江县| 林甸县| 偏关县|