宁静的Thinking

认真工作,及时总结
随笔 - 3, 文章 - 0, 评论 - 0, 引用 - 0
数据加载中……

2012年10月30日

程序员必读书目

     摘要: 月光博客6月12日发表了《写给新手程序员的一封信》,翻译自《An open letter to those who want to start programming》,我的朋友(他在本站的id是Mailper)告诉我,他希望在酷壳上看到一篇更具操作性的文章。因为他也是喜欢编程和技术的家伙,于是,我让他把他的一些学习Python和Web编程的一些点滴总结一下。于是他给我发来了一些他的心得和经历,我...  阅读全文

posted @ 2012-10-30 17:27 orangle_lzz 阅读(267) | 评论 (0)编辑 收藏

2012年10月18日

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 阅读(169) | 评论 (0)编辑 收藏

2012年9月11日

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 阅读(186) | 评论 (0)编辑 收藏