集合中元素大小的可排序问题
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>这个 泛型,那存的时候就一定都是手机了,我在向外取的时候,就只会给客户苹果手机,这样。就是一个合理的过程了。
比较能理解接口泛型,和方法泛型,至于类上面的泛型完全可以通过 参数传过来,没想好。这个 例程做的也比较奇怪,希望可以一起讨论。