




版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、/*Comparable<T> 接口中有如下方法:int compareTo(T o)要想限制 傳入Comparable接口的數(shù)據(jù)類(lèi)型,可以利用泛型來(lái)實(shí)現(xiàn),具體如下:class Student implements Comparable<Student>.public int compareTo(Student ob) /注意Student不能寫(xiě)成了Object*/import java.util.*;class Student implements Comparable<Student>private int id;private String name;
2、private int age;public Student()public Student(int id, String name, int age)this.id = id; = name;this.age = age;Overridepublic String toString()return id + " " + name + " " + age;/形參不能寫(xiě)成Object ob 必須得寫(xiě)為 Student ob 否則編譯時(shí)會(huì)報(bào)錯(cuò),出錯(cuò)信息是:“Student 不是抽象的,并且未覆蓋 java.lang.Comparable 中
3、的抽象方法 compareTo(Student)”/public int compareTo(Object ob) /本方法是錯(cuò)誤的/Student s = (Student)ob;/return this.id = s.id;/本方法是正確的public int compareTo(Student s)return this.id - s.id;public class TestGenerics_1public static void main(String args)List<Student> li = new ArrayList<Student>();li.add
4、(new Student(1000, "zhangsan", 55);li.add(new Student(1005, "lisi", 15);li.add(new Student(1003, "wangwu", 32);li.add(new Student(1001, "xiaojuan", 65);System.out.println(li);Collections.sort(li);System.out.println(li);/*2009年4月3日11:37:19實(shí)現(xiàn)泛型之后的程序*/import java
5、.util.*;class Studentprivate int id;private String name;private int age;public Student()public Student(int id, String name, int age)this.id = id; = name;this.age = age;public int hashCode()return .hashCode()*id*age;public boolean equals(Object o)Student s = (Student)o;return this.n
6、ame.equals() && this.id=s.id && this.age=s.age;public String toString()return id + " " + name + " " + age;public class TestGenerics_2public static void main(String args)HashMap<Integer, Student> hm = new HashMap<Integer, Student>();hm.put(1001, n
7、ew Student(1001, "zhangsan", 20);hm.put(1003, new Student(1003, "lisi", 30);hm.put(1004, new Student(1004, "wangwu", 10);hm.put(1002, new Student(1002, "baichi", 20);/遍歷所有的元素System.out.println("hm容器中所有的元素是:");Set<Integer> s = hm.keySet();Iterat
8、or<Integer> it = s.iterator();while (it.hasNext()int Key = (Integer)it.next(); / (Integer) 不能省 System.out.println(hm.get(Key);System.out.println("直接查找某一元素");System.out.println( hm.get(1003) );System.out.println( hm.get(1005) ); /如果找不到 則直接返回null public class MyKey implements Comparabl
9、eprivate final int id;public MyKey(int id)this.id = id;public int getId()return id;Overridepublic int compareTo(Object o)return this.id - (MyKey)o).id;/默認(rèn)equasl方法比較的是:是否是同一快內(nèi)存,是返回true,不是返回false,但實(shí)際需求通常是不管是否是同一塊內(nèi)存,只要內(nèi)容一樣,不同內(nèi)存相同內(nèi)容的類(lèi)對(duì)象調(diào)用equals方法返回值也應(yīng)該是true,所以本程序重寫(xiě)equals方法是十分必要的,否則會(huì)導(dǎo)致編譯器認(rèn)為MyKey mk1 = ne
10、w MyKey(2); MyKey mk1 = new MyKey(2); mk1和mk2不相等,最終會(huì)導(dǎo)致容器中出現(xiàn)了重復(fù)元素,即容器中同時(shí)出現(xiàn)了mk1 和mk2這兩個(gè)元素,站在用戶(hù)的角度,用戶(hù)會(huì)認(rèn)為容器中出現(xiàn)了重復(fù)元素Overridepublic boolean equals(Object o)return (o instanceof MyKey) && (this.id = (MyKey)o).id);/如果注釋掉了這里的hashCode方法,則會(huì)導(dǎo)致占用不同內(nèi)存但是內(nèi)容一樣的兩個(gè)MyKey對(duì)象的hahsCode()返回值是一樣的Overridepublic int ha
11、shCode()return new Integer(id).hashCode(); /已知: Integer it1 = new Integer(22); Integer it2 = new Integer(22); 則it1.hashCode()的值和it2.hashCode()的值是一樣的,可以參見(jiàn)“C:Documents and Settingshb桌面hashCodeTestHashCode_1.java”public class Personprivate String name;private int age;public Person(String name,int age)t
12、 = name;this.age = age;public void setName(String name) = name;public String getName()return name;public void setAge(int age)this.age = age; public int getAge()return age;public String toString()return "Name: " + name + "tAge: " + age; import java.util.Set;import
13、 java.util.TreeMap;import java.util.Iterator;import java.util.*;public class TestHashpublic static void main(String args) Map hm = new Hashtable(); /針對(duì)HashMap而言:只要沒(méi)有重寫(xiě)鍵類(lèi)MyKey中的hashCode 和 equasl方法中的任何一個(gè)方法,都會(huì)導(dǎo)致HashMap中出現(xiàn)重復(fù)映射,即會(huì)出現(xiàn)多個(gè)"1001 Nancy 49" /針對(duì)Hashtable而言:只要沒(méi)有重寫(xiě)鍵類(lèi)MyKey中的hashCode 和 equa
14、sl方法中的任何一個(gè)方法,都會(huì)導(dǎo)致HashMap中出現(xiàn)重復(fù)映射,即會(huì)出現(xiàn)多個(gè)"1001 Nancy 49" /但是針對(duì)TreeMap而言,至少在本程序中, 是否重寫(xiě)鍵類(lèi)MyKey中的hashCode 和 equasl方法,是重寫(xiě)一個(gè)還是重寫(xiě)兩個(gè)還是兩個(gè)都不重寫(xiě),整個(gè)程序的輸出結(jié)果沒(méi)有任何影響,我個(gè)人估計(jì)是因?yàn)門(mén)reeMap容器已經(jīng)要求所有的鍵類(lèi)都要實(shí)現(xiàn)Comparable接口,這時(shí)候可能hashCode方法和equasl方法都用不到了,當(dāng)然這只是我個(gè)人猜測(cè)而已! hm.put(new MyKey(1003),new Person("Tom",15);hm
15、.put(new MyKey(1008),new Person("Billy",25);hm.put(new MyKey(1005),new Person("Kity",73); hm.put(new MyKey(1001),new Person("Nancy",49);hm.put(new MyKey(1001),new Person("Nancy",49);hm.put(new MyKey(1001),new Person("Nancy",49);hm.put(new MyKey(1001)
16、,new Person("Nancy",49);System.out.println("-檢索單個(gè)元素-");Person p = (Person)hm.get(new MyKey(1008);System.out.println(p);System.out.println("-遍歷所有元素-");Set names = hm.keySet();Iterator it = names.iterator();while(it.hasNext()MyKey key = (MyKey)it.next();Person value = (Pe
17、rson)hm.get(key); System.out.println(key.getId() + "t" + value);System.out.println("hm.size() = " + hm.size();/*在JDK 1.6中的運(yùn)行結(jié)果是:-檢索單個(gè)元素-Name: Billy Age: 25-遍歷所有元素-1008 Name: Billy Age: 251005 Name: Kity Age: 731003 Name: Tom Age: 151001 Name: Nancy Age: 49hm.size() = 4-*/*2009年2月
18、20日18:28:31String 和 Integer 這些Java自帶的類(lèi)都重寫(xiě)了hashCode方法,如果String 和 Integer new出來(lái)的對(duì)象的內(nèi)容是一樣的,則這些對(duì)象的hashCode返回值也是一樣的,雖然這些對(duì)象占用的是不同的內(nèi)存不過(guò)用戶(hù)自定義類(lèi)型則不同,如本程序的A類(lèi),即便是兩個(gè)內(nèi)容一模一樣的A類(lèi)對(duì)象,它們返回的hashCode值也是不一樣的,但是兩個(gè)內(nèi)容一模一樣的Integer類(lèi)對(duì)象或者String類(lèi)對(duì)象返回的hashCode值卻是一樣的,因?yàn)橄到y(tǒng)自帶的String 和 Integer 類(lèi)都已經(jīng)重寫(xiě)了Object的hashCode方法嘛!如果程序員希望自己定義的類(lèi)對(duì)象
19、,占用不同內(nèi)存空間但內(nèi)容卻是一樣的對(duì)象調(diào)用hashCode方法返回值是一樣的,則程序員就必須自己重寫(xiě)hashCode方法,如本程序的B類(lèi)*/class Aprivate int id;public A(int id)this.id = id;class Bprivate int id;public B(int id)this.id = id;Overridepublic int hashCode()return new Integer(id).hashCode();public class TestHashCode_1public static void main(String args) S
20、ystem.out.println("new A(1).hashCode() = " + new A(1).hashCode();System.out.println("new A(1).hashCode() = " + new A(1).hashCode();System.out.println();System.out.println("new Integer(1).hashCode() = " + new Integer(1).hashCode();System.out.println("new Integer(1).
21、hashCode() = " + new Integer(1).hashCode();System.out.println();System.out.println("new String("haha").hashCode() = " + new String("haha").hashCode();System.out.println("new String("haha").hashCode() = " + new String("haha").hashCode()
22、;System.out.println();System.out.println(""haha".hashCode() = " + "haha".hashCode();System.out.println(""haha".hashCode() = " + "haha".hashCode();System.out.println();System.out.println("new B(1).hashCode() = " + new B(1).hashCode
23、();System.out.println("new B(1).hashCode() = " + new B(1).hashCode();Integer it1 = new Integer(1);Integer it2 = new Integer(1);System.out.println(it1 = it2);System.out.println(it1.equals(it2);System.out.println(it1.hashCode() = it2.hashCode();/*在JDK 1.6中的運(yùn)行結(jié)果是:-new A(1).hashCode() = 145768
24、77new A(1).hashCode() = 12677476new Integer(1).hashCode() = 1new Integer(1).hashCode() = 1new String("haha").hashCode() = 3194802new String("haha").hashCode() = 3194802"haha".hashCode() = 3194802"haha".hashCode() = 3194802new B(1).hashCode() = 1new B(1).hashCo
25、de() = 1falsetruetrue-*/*2008年11月17日14:57:12Student類(lèi)必須同時(shí)實(shí)現(xiàn)equals方法 和 hashCode方法 才可以保證在裝入HashSet類(lèi)時(shí)不會(huì)出現(xiàn)“重復(fù)”裝入的情況重新實(shí)現(xiàn)了equals方法 和 hashCode 方法 的正確的程序*/import java.util.*;public class TestHashSetpublic static void main(String args)Collection c = new HashSet();c.add(new Student(1001, "張三");c.add(
26、new Student(1002, "李四");c.add(new Student(1003, "王五"); /10行c.add(new Student(1003, "王五");c.add(new Student(1003, "王五");c.add(new Student(1003, "王五");c.add(new Student(1003, "王五"); /14行Iterator i = c.iterator();while (i.hasNext()System.out.p
27、rintln(i.next();class Studentprivate int num;private String name;public Student()public Student(int num, String name)this.num = num; = name;public String toString()return "學(xué)號(hào): " + this.num + ", 姓名: " + name;public boolean equals(Object o)Student s = (Student)o;return thi
28、s.num=s.num && .equals();public int hashCode()/return num; /如果你設(shè)定的學(xué)生信息中學(xué)號(hào)是唯一的,則可以直接用num來(lái)作為哈希碼return num * .hashCode();/*在JDK 1.6中的運(yùn)行結(jié)果是:-學(xué)號(hào): 1002, 姓名: 李四學(xué)號(hào): 1003, 姓名: 王五學(xué)號(hào): 1001, 姓名: 張三-總結(jié):必須同時(shí)實(shí)現(xiàn)equals()方法 和 hashCode() 方法,只要有任意一個(gè)方法沒(méi)有實(shí)現(xiàn)裝入時(shí)就會(huì)出現(xiàn)重復(fù)元素*/class Apublic int i;
29、public A(int i)this.i = i;public boolean equals(Object ob)A aa = (A)ob;return this.i = aa.i;/public int hashCode()/class Mpublic static void main(String args)A aa1 = new A(2);A aa2 = new A(2);if (aa1 = aa2)System.out.println("aa1 = aa2");elseSystem.out.println("aa1 != aa2");Syste
30、m.out.println(aa1.equals(aa2); /System.out.println(aa1.hashCode();import java.util.*;class Apublic String toString()return "哈哈"public class Testpublic static void main(String args)ArrayList al = new ArrayList();al.add(12345);al.add("張三"); /"張三".length() "張三".c
31、onpareTo("李四");al.add(66.66); /double Doubleal.add(new A();/System.out.println(al2); /容器不是數(shù)組System.out.println(al.get(2); Object obArr = al.toArray();System.out.println(obArr2); /System.out.println(al); import java.util.*;public class TestCollectionpublic static void main(String args)Colle
32、ction c = new LinkedList();c.add(new Student("zhangsan", 80);c.add(66.6);System.out.println(c);class Student private String name;private int age;public Student(String name, int age) = name;this.age = age;/如果把toString方法注釋掉了,則程序輸出結(jié)果會(huì)有亂碼public String toString()return name + " &q
33、uot; + age; /*2008年11月20日12:08:28測(cè)試Collections類(lèi)的使用*/import java.util.*;public class TestCollectionspublic static void main(String args)List lt = new LinkedList(); for (int i=0; i<7; +i)lt.add("a" + i);System.out.println(lt);Collections.shuffle(lt); /記住LinkedList中是沒(méi)有shuffle方法的,因此需要通過(guò)Coll
34、ections類(lèi)的相關(guān)方法來(lái)實(shí)現(xiàn)System.out.println(lt);Collections.sort(lt); /默認(rèn)升序排序,要降序很簡(jiǎn)單,先調(diào)用Collections.sort(); 再調(diào)用Collections.reverse()System.out.println(lt);Collections.reverse(lt); /倒置System.out.println("倒置之后: " + lt);System.out.println(Collections.binarySearch(lt, "a5"); /因?yàn)閘t默認(rèn)不是升序排序的,所以
35、調(diào)用Collections.binarySearch()方法是不會(huì)成功的Collections.sort(lt);System.out.println("重新排序之后: " + lt);System.out.println(Collections.binarySearch(lt, "a5"); /記住,使用binarySearch()方法的前提是該容器已升序排序/*在JDK 1.6中的運(yùn)行結(jié)果是:-a0, a1, a2, a3, a4, a5, a6a5, a3, a6, a4, a2, a0, a1a0, a1, a2, a3, a4, a5, a6倒
36、置之后: a6, a5, a4, a3, a2, a1, a0-8重新排序之后: a0, a1, a2, a3, a4, a5, a65-*/import java.util.*;class Student implements Comparableprivate int id;private String name;public Student(int id, String name)this.id = id; = name;Overridepublic String toString()return id + " " + name; /1000張三 /
37、System.out.println();Overridepublic int compareTo(Object o)Student st = (Student)o;if (this.id = st.id)return 0;else if (this.id > st.id)return 1;elsereturn -1;public class TestListpublic static void main(String args)List L = new ArrayList();L.add(new Student(1000, "張三");L.add(new Stude
38、nt(1003, "小娟");L.add(new Student(1002, "王五");L.add(new Student(1001, "李四");Collections.sort(L);System.out.println(L); /import java.util.*;class Student private int id;private String name;public Student(int id, String name)this.id = id; = name;Overridepublic Str
39、ing toString()return id + " " + name; /1000張三 /System.out.println();public boolean equals(Object ob)Student st = (Student)ob;return st.id=this.id && =;public int hashCode()return id * .hashCode();public class TestSetpublic static void main(String args)Set S
40、 = new HashSet(); /TreeSetS.add(new Student(1000, "張三");S.add(new Student(1003, "小娟");S.add(new Student(1002, "王五");S.add(new Student(1001, "李四");S.add(new Student(1001, "李四");S.add(new Student(1001, "李四");S.add(new Student(1001, "李四&q
41、uot;);S.add(new Student(1001, "李四");S.add(new Student(1001, "李四");System.out.println(S); /public class MyKey implements Comparableprivate final int id;public MyKey(int id)this.id = id;public int getId()return id;Overridepublic int compareTo(Object o)return this.id - (MyKey)o).id;
42、/默認(rèn)equasl方法比較的是:是否是同一快內(nèi)存,是返回true,不是返回false,但實(shí)際需求通常是不管是否是同一塊內(nèi)存,只要內(nèi)容一樣,不同內(nèi)存相同內(nèi)容的類(lèi)對(duì)象調(diào)用equals方法返回值也應(yīng)該是true,所以本程序重寫(xiě)equals方法是十分必要的,否則會(huì)導(dǎo)致編譯器認(rèn)為MyKey mk1 = new MyKey(2); MyKey mk1 = new MyKey(2); mk1和mk2不相等,最終會(huì)導(dǎo)致容器中出現(xiàn)了重復(fù)元素,即容器中同時(shí)出現(xiàn)了mk1 和mk2這兩個(gè)元素,站在用戶(hù)的角度,用戶(hù)會(huì)認(rèn)為容器中出現(xiàn)了重復(fù)元素Overridepublic boolean equals(Object o)r
43、eturn (o instanceof MyKey) && (this.id = (MyKey)o).id);/如果注釋掉了這里的hashCode方法,則會(huì)導(dǎo)致占用不同內(nèi)存但是內(nèi)容一樣的兩個(gè)MyKey對(duì)象的hahsCode()返回值是一樣的Overridepublic int hashCode()return new Integer(id).hashCode(); /已知: Integer it1 = new Integer(22); Integer it2 = new Integer(22); 則it1.hashCode()的值和it2.hashCode()的值是一樣的,可以
44、參見(jiàn)“C:Documents and Settingshb桌面hashCodeTestHashCode_1.java”public class Personprivate String name;private int age;public Person(String name,int age) = name;this.age = age;public void setName(String name) = name;public String getName()return name;public void setAge(int age)this.age = age; public int getAge()return age;public String toString()return "Name: " + name + "tAge: " + age; import java.util.Set;import java.ut
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 期中測(cè)試08 -2020-2021學(xué)年七年級(jí)地理下學(xué)期期末專(zhuān)項(xiàng)復(fù)習(xí)(中圖版)(解析版)
- 2025商業(yè)用地轉(zhuǎn)讓合同模板
- Unit 6 Useful numbers單元整體教學(xué)設(shè)計(jì)表格式-2024-2025學(xué)年人教PEP版(2024)英語(yǔ)三年級(jí)上冊(cè)
- 一年級(jí)道德與法治下冊(cè) 第二單元 我的手兒巧 第5課《做個(gè)小玩意兒》教學(xué)設(shè)計(jì)1 教科版
- 金融欺詐行為識(shí)別方案
- 多語(yǔ)言支持模塊開(kāi)發(fā)指南
- 2025年勞動(dòng)合同的違約金標(biāo)準(zhǔn)是多少才合適
- 2025國(guó)家教育資助貸款合同
- 《整數(shù)乘法運(yùn)算律推廣到小數(shù)》(教學(xué)設(shè)計(jì))-2024-2025學(xué)年五年級(jí)上冊(cè)數(shù)學(xué)人教版
- 2025年上海貨運(yùn)從業(yè)資證孝試模似題庫(kù)
- 卡西歐手表EFA-120中文使用說(shuō)明書(shū)
- 加油站變更管理制度
- 75%食用酒精安全技術(shù)說(shuō)明書(shū)(MSDS)
- -小學(xué)英語(yǔ)人稱(chēng)代詞與物主代詞講解課件(共58張課件).課件
- 醫(yī)學(xué)課件疼痛的護(hù)理
- 船舶采購(gòu)建造 投標(biāo)方案(技術(shù)方案)
- 走近湖湘紅色人物智慧樹(shù)知到答案2024年湖南工商大學(xué)
- 2024年初級(jí)養(yǎng)老護(hù)理員職業(yè)鑒定考試題庫(kù)(含答案)
- 模塊21.CR400AF型動(dòng)車(chē)組轉(zhuǎn)向架 《高速鐵路動(dòng)車(chē)組機(jī)械設(shè)備維護(hù)與檢修》教學(xué)課件
- GGD交流低壓配電柜運(yùn)行、維護(hù)說(shuō)明書(shū)、安裝、操作手冊(cè)
- 多發(fā)性骨髓瘤腎損傷診治指南(2024版)
評(píng)論
0/150
提交評(píng)論