The example of this article tells the common method of List objects in Java. Share it for everyone for your reference. The specific analysis is as follows:
In the list list detected in the database, different fields are often sorted. The general approach is to use sorted fields and re -check in the database. If not inquiring in the database, sorting directly in the first found List will undoubtedly improve the performance of the system.
As long as the first detected results are stored in session, you can sort the list. Generally, the list sorting can be used in collections.sort (list), but if the list contains an object, this method is still not possible. So how do you sort it? If there is an UserInfo object, it contains the following field:
Private java.lang.integer userId; private java.lang.string username; private java.util.date bithdate; private java.lang.integer agon;
So now you need to sort Userid, you may use the following method:
Collections.sort(list, new Comparator() { public int compare(Object a, Object b) { int one = ((Order)a).getUserId (); int two = ((Order)b).getUserId (); Return One-Two;}});
In this way, if you want to sort each field of the UserInfo list, is it written by a segment as shown above in each field? That is not the result we need. Writing a program is more and more refined, and you cannot write more and more redundant. Can you write a general method? The answer is yes, but first of all, we must solve the following three questions:
1. Can use generic type;
2. Being a common comparison method, such as compareto;
3. Is there a pan -way method like generic and generic methods?
The first problem can be solved. The second problem is not very difficult, because all types of Java inherit Object. There is a Tostring method. For the time being, you can convert all types to string, and then compare it with compareto. The third issue has no pan -way method we need. But can we change it and use the GetMethod and Invoke methods to dynamically take out methods. The code is as follows:
Public Class Sortlist <E> {Public Void Sort (list <E> List, Final String Method, Final String Sort) {Collections.sort (List, New Comparator () {Public Int Compare (O. bky a, object b) {int Ret = 0; TRY {Method M1 = ((E) a) .getClass (). GetMethod (metHod, NULL); Method M2 = ((E) B) .getClass (). ! = NULL && "Desc" .equals (sort) // The inverted (((((((e) b), null) .tring (). Compareto (((E) a), null ) .tring ()); Else // Positive order RET = m1.invoke ((((E) a), null) .tring (). Compareto (m2.invoke ((E) B), null) .tring ( );} Catch (noSuchMethodexception ne) {system.out.println (ne);} Catch (illegalaccessException IE) {system.out.println (IE); ATINTARGETEXCEPTION it) {System.out.println (IT) ;} Return Ret;}});}}
Looking at the above code, did we successfully solve the above three problems and add the upside -down order. There are no specific objects and types in the code, and it is already versatile. We use a generic E. If you want to sort the userId of UserInfo, you can pass the method name in the form of a string in the form of parameters: for example, "for example" GetUserid ". You can use the code provided below to test:
//Test.javapackage test; Import Java.util.arrayList; Import Java.util.List; Import Java.text.simpleDateFormat; Public Class Test {Public Static ID main (string [] args) throws exception {list <userinfo> list = new ArrayList<UserInfo>(); SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd"); list.add(new UserInfo(3,"b",formater.parse("1980-12-01"), 11)); list.add (new userInfo (1, "c", formterer.parse ("" 1980-10-01 "), 30); ("1973-10-01"), 11)); System.out.println ("------- The original sequence ----------------------------------------------------------" "" " ); free (userInfo user: list) {system.out.println (user.tostring ());} // Call the universal class Sortlist <userInfo> sortlist = new sortlist <userinfo> (); sortlist .Sort (list, "GetUserid", "DESC"); System.out.println ("----------------------------------------------- "); For (userInfo user: list) {system.out.println (user.tostring ());} // Press username sortlist.sort (list," getusername ", null); System.out.println " --------- Sort by username ------------------------------------------------------------------------------------------------------- "); );} // SortList.sort (list, "getBITHDATESTR", NULL); System.out.println ("-------------------------------- -------------------------- "); for (userInfo user: list) {system.out.println (user.tostring ());}}}
The test results are as follows:
------- Original sequence --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 3; B; 1980-12-01; 111; C; 1980-10-01; 302; 302; 302; 302; a; 1973-10-01; 11--------按userId倒序------------------3; b; 1980-12-01; 112; a; 1973-10-01; 111; c; 1980-10-01; 30---------按username排序-----------------2; A; 1973-10-01; 113; B; 1980-12-01; 111; C; 1980-10-01; 30 ------------------------------------------------------------------------------------------------------------------------------------------------ -------- 2; A; 1973-10-01; 111; C; 1980-10-01; 303; B; 1980-12-01; 11
Note: The sorting of the date is first transformed by the format, otherwise there will be no correct result.
It is hoped that this article is helpful to everyone's Java program design.