package CollectionPart; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class CollectionSort { public static v
package CollectionPart; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class CollectionSort { public static void main(String[] args) { List myList = new ArrayList(); myList.add("3"); myList.add("1"); myList.add("5"); myList.add("sdf"); myList.add("fwef"); Collections.sort(myList); for (Object object : myList) { System.out.println(object); } //這樣,集合就幫助我們進行了排序的操作。 System.out.println("------------"); //依然是,之前遇到的那個問題。如果不再是字元串或者基本數據類型,如果是我們自己定義的一個實體對象類型,那麼如何比較兩個元素的大小呢? //剛剛有寫過一個 員工2類,那裡面重寫了equals() 和hashCode() 方法,那不妨試試員工2類。 Employee_3 e1 = new Employee_3("1","lifei",23,"2016-06-03"); Employee_3 e2 = new Employee_3("2","lifei2",24,"2016-06-03"); Employee_3 e3 = new Employee_3("3","lifei3",25,"2016-06-03"); List<Employee_3> myList2 = new ArrayList(); myList2.add(e3); myList2.add(e1); myList2.add(e2); Collections.sort(myList2); for (Employee_3 employee_2 : myList2) { System.out.println(employee_2); } System.out.println("從輸出結果上很容易看出來,是按照我們添加的順序輸出的,事實上我們也沒有進行排序.從結果上看原來的員工2類,改成了現在的3.這是因為,在寫sort的時候,就不允許了,這是因為編譯器,在編譯過程中發現,當前類也就是 employee_2中並不具有比較器。"); /** * 所以,如果想要對一個我們自己實現的對象進行排序的話,必須要讓被排序的實體擁有一個比較器。也就是說對於我們所寫的實體如果讓他符合set集合的話,需要告訴編譯器到底什麼相等,兩個實體才算相等,為了讓相同類型的元素進行排序,我們需要指定怎麼樣才算是大,怎麼樣才算是小。 * 這樣就需要一個,比較器,有兩種實現形式,方式1:讓實體類實現comparable 介面。 * 同是為了排序,如果實體類已經不可更改,或者 最好不用去觸碰原來的代碼,那麼重寫一個,比較器類也是不錯的選擇。 */ Collections.sort(myList2,new MyComparator()); for (Employee_3 employee_3 : myList2) { System.out.println(employee_3); } } }
員工3類:
package CollectionPart; /** * 實現了 comparable介面 * @param <T> * */ public class Employee_3 implements Comparable<Employee_3> { private String employeeId; private String employeeName; private int employeeAge; private String employeeHireDate; /* 這裡就存儲成String 類型,在 資料庫裡面設置成 date格式。 然後 利用Date today = new Date(); SimpleDateFormat fm = new SimpleDateFormat("YYYY-MM-dd"); 來做 */ public String getEmployeeId() { return employeeId; } public void setEmployeeId(String employeeId) { this.employeeId = employeeId; } public String getEmployeeName() { return employeeName; } public void setEmployeeName(String employeeName) { this.employeeName = employeeName; } public int getEmployeeAge() { return employeeAge; } public void setEmployeeAge(int employeeAge) { this.employeeAge = employeeAge; } public String getEmployeeHireDate() { return employeeHireDate; } public void setEmployeeHireDate(String employeeHireDate) { this.employeeHireDate = employeeHireDate; } @Override public String toString() { return "Employee_1 [employeeId=" + employeeId + ", employeeName=" + employeeName + ", employeeAge=" + employeeAge + ", employeeHireDate=" + employeeHireDate + "]"; } public Employee_3(String employeeId, String employeeName, int employeeAge, String employeeHireDate) { super(); this.employeeId = employeeId; this.employeeName = employeeName; this.employeeAge = employeeAge; this.employeeHireDate = employeeHireDate; } public Employee_3() { super(); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + employeeAge; result = prime * result + ((employeeHireDate == null) ? 0 : employeeHireDate.hashCode()); result = prime * result + ((employeeId == null) ? 0 : employeeId.hashCode()); result = prime * result + ((employeeName == null) ? 0 : employeeName.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Employee_3 other = (Employee_3) obj; if (employeeAge != other.employeeAge) return false; if (employeeHireDate == null) { if (other.employeeHireDate != null) return false; } else if (!employeeHireDate.equals(other.employeeHireDate)) return false; if (employeeId == null) { if (other.employeeId != null) return false; } else if (!employeeId.equals(other.employeeId)) return false; if (employeeName == null) { if (other.employeeName != null) return false; } else if (!employeeName.equals(other.employeeName)) return false; return true; } @Override public int compareTo(Employee_3 o) { return this.getEmployeeAge()-o.getEmployeeAge(); } }
比較器:
package CollectionPart; import java.util.Comparator; public class MyComparator implements Comparator<Employee_3> { @Override public int compare(Employee_3 o1, Employee_3 o2) { return o2.getEmployeeAge()-o1.getEmployeeAge(); } }
運行結果:
1
3
5
fwef
sdf
------------
Employee_1 [employeeId=1, employeeName=lifei, employeeAge=23, employeeHireDate=2016-06-03]
Employee_1 [employeeId=2, employeeName=lifei2, employeeAge=24, employeeHireDate=2016-06-03]
Employee_1 [employeeId=3, employeeName=lifei3, employeeAge=25, employeeHireDate=2016-06-03]
從輸出結果上很容易看出來,是按照我們添加的順序輸出的,事實上我們也沒有進行排序.從結果上看原來的員工2類,改成了現在的3.這是因為,在寫sort的時候,就不允許了,這是因為編譯器,在編譯過程中發現,當前類也就是 employee_2中並不具有比較器。
Employee_1 [employeeId=3, employeeName=lifei3, employeeAge=25, employeeHireDate=2016-06-03]
Employee_1 [employeeId=2, employeeName=lifei2, employeeAge=24, employeeHireDate=2016-06-03]
Employee_1 [employeeId=1, employeeName=lifei, employeeAge=23, employeeHireDate=2016-06-03]
都到了這裡了,發現泛型問題不可迴避。
我們發現在第一個輸出結果裡面,既可以存放數字也可以存放字元串兒,這是由於java在封裝的時候為我們提供了便利,希望我們可以存儲任何類型的數據到集合中,但是這就會帶來一定的弊端,比如這個集合裡面什麼都可以放的話,對於我們針對某些特定的元素就會出現問題。比如我們要是只能向集合中添加數字,那麼存取的時候都知道這裡是數字,並且方便易用。但是因為沒有加限定條件,導致了數字的集合裡面放入了別的元素。這樣存取和使用都會出現問題,就好像我有很多台蘋果手機,然後有人跟我買,我開始都掏出手機給對方,但是由於沒有加約束,導致了工人在存放數據的時候,放入了可以食用的蘋果,這個時候我在取的時候拿給買家一個可以食用的蘋果,而別人給我了幾千塊錢是想買一部手機,這就會出問題。人家可能會打我。這就是在存放的時候,沒有加約束的結果,如果往倉庫的存儲過程中,限定了只能放入手機,而不是水果,那麼加入了<Mobile>這個 泛型,那存的時候就一定都是手機了,我在向外取的時候,就只會給客戶蘋果手機,這樣。就是一個合理的過程了。
比較能理解介面泛型,和方法泛型,至於類上面的泛型完全可以通過 參數傳過來,沒想好。這個 常式做的也比較奇怪,希望可以一起討論。