很實用的一個List集合排序類。(本作品為原創,如有不足之處,還望大牛多給意見。如需轉載,請注明出處。謝謝!)
一、根據List<Student> 中的Student對象中的開始時間進行排序。注意,該類只給Date類型屬性字段進行排序。代碼如下:
說明:
自定義類名:DateSortList ,方法名:sortByAttribute,參數1:List,即你需要進行排序的List。參數2:String attribute,即你需要進行排序的字段,Student對象中的時間屬性,例如:開始時間-->startDate屬性。當然也可以為數據的創建時間。只要是Date類型參數即可。參數3:boolean reverseFlag ,排序規則。true為倒序。。
public class DateSortList { private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); /** * 對列表中的數據按指定日期字段進行排序。 * * @param list * @param attribute 排序的時間字段,例如Vo類中的開始時間字段:startDate; * @param reverseFlag true:倒序 */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static void sortByAttribute(List list, final String attribute,final boolean reverseFlag) { Collections.sort(list, new Comparator<Object>() { public int compare(Object arg1, Object arg2) { int result = 0; try { Field f1 = arg1.getClass().getDeclaredField(attribute); f1.setAccessible(true); Field f2 = arg2.getClass().getDeclaredField(attribute); f2.setAccessible(true); String s1 = DateUtil.convertDateToString( (Date) f1.get(arg1), "yyyy-MM-dd"); String s2 = DateUtil.convertDateToString( (Date) f2.get(arg2), "yyyy-MM-dd"); if (s1 == null || s1.equals("")) { s1 = "1900-00-00"; } if (s2 == null || s2.equals("")) { s2 = "1900-00-00"; } long l = sdf.parse(s1).getTime() - sdf.parse(s2).getTime(); if (l > 0) { result = 1; } else if (l < 0) { result = -1; } else { result = 0; } if (reverseFlag) { // 倒序 result = -result; } } catch (Exception e) { e.printStackTrace(); } return result; } }); } }
二、根據List<Student> 中的Student對象中的Int、Date、Long三種類型屬性字段進行排序,代碼如下:
說明:該方法是在上面的方法中進行拓展,既然可以根據Date類型進行排序,那么其他類型呢?這里拓展了Int、long,兩種類型。
自定義類名:DateSortList ,方法名:sortByAttribute,參數1:List,即你需要進行排序的List。參數2:String attribute,即你需要進行排序的字段,Student對象中的時間屬性,例如:開始時間-->startDate屬性。當然也可以為數據的創建時間。只要是Date類型參數即可。參數3:boolean reverseFlag ,排序規則。true為倒序。參數4: String type
即為需要進行排序字段的類型。這里拓展了Int、long,兩種類型。
/** * 對列表中的數據按指定字段類型進行排序。 * * @param list * 需要排序的列表 * @param attribute * 字段id * @param reverseFlag * 是否倒序 * @param type:
* Int、Date、Long */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static void sortByAttribute(List list, final String attribute,final boolean reverseFlag, final String type) { Collections.sort(list, new Comparator<Object>() { public int compare(Object arg1, Object arg2) { int result = 0; try { Field f1 = arg1.getClass().getDeclaredField(attribute); f1.setAccessible(true); Field f2 = arg2.getClass().getDeclaredField(attribute); f2.setAccessible(true); if ("date".equals(type)) { String s1 = (String) f1.get(arg1); String s2 = (String) f2.get(arg2); if (s1 == null || s1.equals("")) { s1 = "1900-00-00"; } if (s2 == null || s2.equals("")) { s2 = "1900-00-00"; } long l = sdf.parse(s1).getTime() - sdf.parse(s2).getTime(); if (l > 0) { result = 1; } else if (l < 0) { result = -1; } else { result = 0; } } else if ("int".equals(type)) { Integer s1 = (Integer) f1.get(arg1); Integer s2 = (Integer) f2.get(arg2); if (s1 == null) { s1 = -9999; } if (s2 == null) { s2 = -9999; } int l = s1 - s2; if (l > 0) { result = 1; } else if (l < 0) { result = -1; } else { result = 0; } } else if ("long".equals(type)) { Long s1 = (Long) f1.get(arg1); Long s2 = (Long) f2.get(arg2); if (s1 == null) { s1 = -9999L; } if (s2 == null) { s2 = -9999L; } long l = s1 - s2; if (l > 0) { result = 1; } else if (l < 0) { result = -1; } else { result = 0; } } else if ("string".equals(type)) { String s1 = (String) f1.get(arg1); String s2 = (String) f2.get(arg2); if (s1 == null || s1.equals("")) { s1 = "0"; } if (s2 == null || s2.equals("")) { s2 = "0"; } int l = Integer.parseInt(s1) - Integer.parseInt(s2); if (l > 0) { result = 1; } else if (l < 0) { result = -1; } else { result = 0; } } if (reverseFlag) { // 倒序 result = -result; } } catch (Exception e) { e.printStackTrace(); } return result; } }); }
三、給List去重且不改變順序,代碼如下:
說明:開發過程中,有很多小伙伴想給一個List集合去掉重復。我們都知道集合中的Set即可去重。
為了方便代碼管理性,通用性。工具類肯定是少不了的。代碼如下:
public static List<String> removeDuplicateWithOrder(List<String> list) { List<String> resultList = new ArrayList<String>(); Set<String> set = new HashSet<String>(); Iterator<String> iterator = list.iterator(); while (iterator.hasNext()) { String element = iterator.next().toString(); if (set.add(element)) { resultList.add(element); } } return resultList; }