




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、/*Comparable<T> 接口中有如下方法:int compareTo(T o)要想限制 傳入Comparable接口的數據類型,可以利用泛型來實現,具體如下:class Student implements Comparable<Student>.public int compareTo(Student ob) /注意Student不能寫成了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;/形參不能寫成Object ob 必須得寫為 Student ob 否則編譯時會報錯,出錯信息是:“Student 不是抽象的,并且未覆蓋 java.lang.Comparable 中
3、的抽象方法 compareTo(Student)”/public int compareTo(Object ob) /本方法是錯誤的/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實現泛型之后的程序*/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;/默認equasl方法比較的是:是否是同一快內存,是返回true,不是返回false,但實際需求通常是不管是否是同一塊內存,只要內容一樣,不同內存相同內容的類對象調用equals方法返回值也應該是true,所以本程序重寫equals方法是十分必要的,否則會導致編譯器認為MyKey mk1 = ne
10、w MyKey(2); MyKey mk1 = new MyKey(2); mk1和mk2不相等,最終會導致容器中出現了重復元素,即容器中同時出現了mk1 和mk2這兩個元素,站在用戶的角度,用戶會認為容器中出現了重復元素Overridepublic boolean equals(Object o)return (o instanceof MyKey) && (this.id = (MyKey)o).id);/如果注釋掉了這里的hashCode方法,則會導致占用不同內存但是內容一樣的兩個MyKey對象的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()的值是一樣的,可以參見“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(); /針對HashMap而言:只要沒有重寫鍵類MyKey中的hashCode 和 equasl方法中的任何一個方法,都會導致HashMap中出現重復映射,即會出現多個"1001 Nancy 49" /針對Hashtable而言:只要沒有重寫鍵類MyKey中的hashCode 和 equa
14、sl方法中的任何一個方法,都會導致HashMap中出現重復映射,即會出現多個"1001 Nancy 49" /但是針對TreeMap而言,至少在本程序中, 是否重寫鍵類MyKey中的hashCode 和 equasl方法,是重寫一個還是重寫兩個還是兩個都不重寫,整個程序的輸出結果沒有任何影響,我個人估計是因為TreeMap容器已經要求所有的鍵類都要實現Comparable接口,這時候可能hashCode方法和equasl方法都用不到了,當然這只是我個人猜測而已! 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("-檢索單個元素-");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中的運行結果是:-檢索單個元素-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自帶的類都重寫了hashCode方法,如果String 和 Integer new出來的對象的內容是一樣的,則這些對象的hashCode返回值也是一樣的,雖然這些對象占用的是不同的內存不過用戶自定義類型則不同,如本程序的A類,即便是兩個內容一模一樣的A類對象,它們返回的hashCode值也是不一樣的,但是兩個內容一模一樣的Integer類對象或者String類對象返回的hashCode值卻是一樣的,因為系統自帶的String 和 Integer 類都已經重寫了Object的hashCode方法嘛!如果程序員希望自己定義的類對象
19、,占用不同內存空間但內容卻是一樣的對象調用hashCode方法返回值是一樣的,則程序員就必須自己重寫hashCode方法,如本程序的B類*/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中的運行結果是:-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類必須同時實現equals方法 和 hashCode方法 才可以保證在裝入HashSet類時不會出現“重復”裝入的情況重新實現了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 "學號: " + 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; /如果你設定的學生信息中學號是唯一的,則可以直接用num來作為哈希碼return num * .hashCode();/*在JDK 1.6中的運行結果是:-學號: 1002, 姓名: 李四學號: 1003, 姓名: 王五學號: 1001, 姓名: 張三-總結:必須同時實現equals()方法 和 hashCode() 方法,只要有任意一個方法沒有實現裝入時就會出現重復元素*/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); /容器不是數組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方法注釋掉了,則程序輸出結果會有亂碼public String toString()return name + " &q
33、uot; + age; /*2008年11月20日12:08:28測試Collections類的使用*/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中是沒有shuffle方法的,因此需要通過Coll
34、ections類的相關方法來實現System.out.println(lt);Collections.sort(lt); /默認升序排序,要降序很簡單,先調用Collections.sort(); 再調用Collections.reverse()System.out.println(lt);Collections.reverse(lt); /倒置System.out.println("倒置之后: " + lt);System.out.println(Collections.binarySearch(lt, "a5"); /因為lt默認不是升序排序的,所以
35、調用Collections.binarySearch()方法是不會成功的Collections.sort(lt);System.out.println("重新排序之后: " + lt);System.out.println(Collections.binarySearch(lt, "a5"); /記住,使用binarySearch()方法的前提是該容器已升序排序/*在JDK 1.6中的運行結果是:-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、/默認equasl方法比較的是:是否是同一快內存,是返回true,不是返回false,但實際需求通常是不管是否是同一塊內存,只要內容一樣,不同內存相同內容的類對象調用equals方法返回值也應該是true,所以本程序重寫equals方法是十分必要的,否則會導致編譯器認為MyKey mk1 = new MyKey(2); MyKey mk1 = new MyKey(2); mk1和mk2不相等,最終會導致容器中出現了重復元素,即容器中同時出現了mk1 和mk2這兩個元素,站在用戶的角度,用戶會認為容器中出現了重復元素Overridepublic boolean equals(Object o)r
43、eturn (o instanceof MyKey) && (this.id = (MyKey)o).id);/如果注釋掉了這里的hashCode方法,則會導致占用不同內存但是內容一樣的兩個MyKey對象的hahsCode()返回值是一樣的Overridepublic int hashCode()return new Integer(id).hashCode(); /已知: Integer it1 = new Integer(22); Integer it2 = new Integer(22); 則it1.hashCode()的值和it2.hashCode()的值是一樣的,可以
44、參見“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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 饅頭特色小吃店行業深度調研及發展項目商業計劃書
- 書法杯墊書寫設計創新創業項目商業計劃書
- 休閑觀光活動在線平臺企業制定與實施新質生產力項目商業計劃書
- 耳鼻喉科中藥針劑行業跨境出海項目商業計劃書
- 大單元教學模式的基本概念與特征解析
- 揚州工業職業技術學院《漆藝與家具陳設設計》2023-2024學年第二學期期末試卷
- 重慶工商職業學院《原畫設計》2023-2024學年第二學期期末試卷
- 能量均分定理題目及答案
- 巖溶地區巖溶勘察報告
- 模擬商業談判題目及答案
- 金屬晶體與離子晶體-(共44張)
- 中國近代史綱要福建農林大學練習題復習資料
- 咨詢服務合同范本格式樣本
- 《畜產品加工與檢測》考試復習題庫(含答案)
- 結核病分子診斷
- 餐飲服務行業食品安全管理人員知識考試題庫(附答案)
- 太陽系中的有趣科學學習通超星期末考試答案章節答案2024年
- 培訓學校收費和退費管理制度
- 法社會學教程(第三版)教學
- 國內外高等教育教材比較研究課題
- 浙江省紹興市諸暨市2023-2024學年五年級下學期期末數學試卷
評論
0/150
提交評論