ListUtil常用操作

    /**
     * 获取列表总页数
     */
    public static <T> int getListPages(List<T> list,int pageNum,int pageSize ){
        if (isNull(list)){
            return 0;
        }
        BaseQuery baseQuery=new BaseQuery();
        baseQuery.setPageNum(pageNum);
        baseQuery.setPageSize(pageSize);
        //list的大小
        int total = list.size();
        baseQuery.setTotal(total);
        return baseQuery.getPages();
    }




    /**
     * 对列表进行分页,索引左边包括,右边不包括
     */
    public static <T> List<T> subListByPage(List<T> list,int pageNum,int pageSize ){
        if (isNull(list)){
            return Collections.emptyList();
        }
        BaseQuery baseQuery=new BaseQuery();
        baseQuery.setPageNum(pageNum);
        baseQuery.setPageSize(pageSize);
        //list的大小
        int total = list.size();
        //对list进行截取
        return list.subList(baseQuery.getStartPosition(),total-baseQuery.getStartPosition()>baseQuery.getPageSize()?baseQuery.getStartPosition()+baseQuery.getPageSize():total);
    }

    /**
     * 对列表进行索引截取,索引左边包括,右边不包括
     */
    public static <T> List<T> subListByPosition(List<T> list,BaseQuery baseQuery){

        if (isNull(list)){
            baseQuery.setTotal(0);
            return Collections.emptyList();
        }
        //设置列表总条数
        int total = list.size();
        baseQuery.setTotal(total);

        if ((baseQuery.getStartIndex()-1)>=total){
            return Collections.emptyList();
        }
        //对list进行截取
        return list.subList(baseQuery.getStartIndex()-1,baseQuery.getEndIndex()>total?total:baseQuery.getEndIndex());
    }


    /**
     *对列表字段进行比较排序
     */
    public static <T> void sortByField(List<T> dtoList,String fieldName,String order) {
        int compare=1;
        if ("desc".equals(order)){
            compare=-1;
        }
        int finalCompare = compare;

        Collections.sort(dtoList, new Comparator<T>() {
            @Override
            public int compare(T o1, T o2) {
                PropertyDescriptor pd1 = null;
                PropertyDescriptor pd2 = null;
                Object value1 =null;
                Object value2 =null;
                try {
                    pd1 = new PropertyDescriptor(fieldName, o1.getClass());
                    value1 = pd1.getReadMethod().invoke(o1, null);

                    pd2 = new PropertyDescriptor(fieldName, o2.getClass());
                    value2 = pd2.getReadMethod().invoke(o2, null);

                } catch (IntrospectionException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }

                 if (value1.getClass().equals(Double.class)){
                    System.out.println(2);
                    if ((Double)value1 > (Double)value2) {
                        return finalCompare;
                    } else if ((Double)value1 < (Double)value2) {
                        return -finalCompare;
                    }
                }else if (value1.getClass().equals(Integer.class)){
                    System.out.println(4);
                    if ((Integer)value1 > (Integer)value2) {
                        return finalCompare;
                    } else if ((Integer)value1 < (Integer)value2) {
                        return -finalCompare;
                    }
                }
                return 0;
            }
        });
    }
原文地址:https://www.cnblogs.com/kesimin/p/9547669.html