Java开发经验分享之如何给List集合进行排序。自定义排序的类,专门针对列表(List)中的日期数据进行排序;也可按指定属性进行。

很实用的一个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即可去重。

为了方便代码管理性,通用性。工具类肯定是少不了的。代码如下:

扫描二维码关注公众号,回复: 5103095 查看本文章
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;

    }

猜你喜欢

转载自www.cnblogs.com/JimYi/p/10330039.html