• 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;
                }
            });
        }
  • 相关阅读:
    python 安装 reportlab 报错 “ImportError: No module named reportlab.lib”
    人声和乐器的频谱范围
    C语言中指针中的值赋值给数组
    快速熟悉Matlab
    Ubuntu 16.04 下octave的使用入门
    IP达人启示录
    python常用的十进制、16进制之间的转换
    GNU 下命令objcopy 用法
    python zeros用法实例
    Debian/Ubuntu清理硬盘空间的8个技巧
  • 原文地址:https://www.cnblogs.com/kesimin/p/9547669.html
Copyright © 2020-2023  润新知