Java開發經驗分享之如何給List集合進行排序。自定義排序的類,專門針對列表(List)中的日期數據進行排序;也可按指定屬性進行。

很實用的一個List集合排序類。(本做品爲原創,若有不足之處,還望大牛多給意見。如需轉載,請註明出處。謝謝!)工具

1、根據List<Student> 中的Student對象中的開始時間進行排序。注意,該類只給Date類型屬性字段進行排序。代碼以下:spa

說明:code

  自定義類名:DateSortList ,方法名:sortByAttribute,參數1:List,即你須要進行排序的List。參數2:String attribute,即你須要進行排序的字段,Student對象中的時間屬性,例如:開始時間-->startDate屬性。固然也能夠爲數據的建立時間。只要是Date類型參數便可。參數3:boolean reverseFlag ,排序規則。true爲倒序。。orm

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; } }); } }

2、根據List<Student> 中的Student對象中的Int、Date、Long三種類型屬性字段進行排序,代碼以下:對象

說明:該方法是在上面的方法中進行拓展,既然能夠根據Date類型進行排序,那麼其餘類型呢?這裏拓展了Int、long,兩種類型。blog

  自定義類名:DateSortList ,方法名:sortByAttribute,參數1:List,即你須要進行排序的List。參數2:String attribute,即你須要進行排序的字段,Student對象中的時間屬性,例如:開始時間-->startDate屬性。固然也能夠爲數據的建立時間。只要是Date類型參數便可。參數3:boolean reverseFlag ,排序規則。true爲倒序。參數4: String type排序

即爲須要進行排序字段的類型。這裏拓展了Int、long,兩種類型。element

/** * 對列表中的數據按指定字段類型進行排序。 * * @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; } }); }

3、給List去重且不改變順序,代碼以下:開發

說明:開發過程當中,有不少小夥伴想給一個List集合去掉重複。咱們都知道集合中的Set便可去重。rem

爲了方便代碼管理性,通用性。工具類確定是少不了的。代碼以下:

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; }
相關文章
相關標籤/搜索