Sorting of element size in a set
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) ;}// in this way, the SET helps us sort. System. out. println ("------------"); // still, the problem encountered earlier. If it is no longer a string or basic data type, and if it is an object type defined by ourselves, how can we compare the sizes of the two elements? // I have just written an employee Class 2, and I have rewritten the equals () and hashCode () methods. Try employee Class 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 ("It is easy to see from the output results that are output in the order we add. In fact, we have not sorted the results. from the result, the original employee category 2 is changed to the current 3. this is because it is not allowed when writing sort. This is because the compiler found that the current class, namely, employee _2, does not have a comparator during the compilation process. ");/*** Therefore, if you want to sort an object we implement, you must have a comparator for the object to be sorted. That is to say, if the entity we write matches the set, we need to tell the compiler what is equal. The two entities are equal. In order to sort elements of the same type, we need to specify how it is big and how it is small. * In this case, a comparator is required. There are two implementation modes: Method 1: Let the entity class implement the comparable interface. * The same is for sorting. If the entity class cannot be changed, or it is better not to touch the original code, rewrite one, and the comparator class is also a good choice. */Collections. sort (myList2, new MyComparator (); for (Employee_3 employee_3: myList2) {System. out. println (employee_3 );}}}
Employees:
Package CollectionPart;/*** implements the comparable interface * @ param <T> **/public class Employee_3 implements Comparable <Employee_3> {private String employeeId; private String employeeName; private int employeeAge; private String employeeHireDate;/* It is stored as String type and set to date format in the database. Then use Date today = new Date (); SimpleDateFormat fm = new SimpleDateFormat ("YYYY-MM-dd"); to do */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 + ", employe EAge = "+ 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 ();}}
Comparator:
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(); }}
Running result:
1
3
5
Fwef
Sdf
------------
Employee_1 [employeeId = 1, employeeName = lifei, employeeAge = 23, employeeHireDate =]
Employee_1 [employeeId = 2, employeeName = lifei2, employeeAge = 24, employeeHireDate =]
Employee_1 [employeeId = 3, employeeName = lifei3, employeeAge = 25, employeeHireDate =]
From the output results, it is easy to see that the output is in the order we add, and in fact we have not sorted it. from the result, the original employee category 2 is changed to the current 3. this is because it is not allowed when writing sort. This is because the compiler found that the current class, namely, employee _2, does not have a comparator during the compilation process.
Employee_1 [employeeId = 3, employeeName = lifei3, employeeAge = 25, employeeHireDate =]
Employee_1 [employeeId = 2, employeeName = lifei2, employeeAge = 24, employeeHireDate =]
Employee_1 [employeeId = 1, employeeName = lifei, employeeAge = 23, employeeHireDate =]
All of them are here, and the problem of generics cannot be avoided.
We found that in the first output result, both numbers and strings can be stored. This is because java provides us with convenience during encapsulation, we hope we can store any type of data to the collection, but this will bring some drawbacks. For example, if everything in this collection can be put, for some specific elements, problems may occur. For example, if we can only add numbers to a collection, we all know that this is a number and it is easy to use. However, because there are no restrictions, other elements are added to the set of numbers. In this way, there will be problems in access and use, as if I have a lot of Apple mobile phones, and then someone buys them with me. I started to pull out the mobile phones for each other, but since there is no constraint, as a result, the workers put edible apples while storing data. At this time, I gave the buyer an edible apple, if someone else gives me thousands of dollars to buy a mobile phone, this will lead to problems. People may beat me. This is the result of no constraints during storage. If the storage process to the warehouse is limited to Mobile phones rather than fruits, then the <Mobile> generic type is added, it must be a mobile phone when it is stored. I will only give the customer an Apple mobile phone when it is retrieved. It is a reasonable process.
You can better understand interface generics and method generics. As for the generic type above the class, you can pass it through parameters without thinking about it. This routine is also quite strange, and I hope it can be discussed together.