這是不是說(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