zoukankan      html  css  js  c++  java
  • 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;
                }
            });
        }
  • 相关阅读:
    lodash kebabCase
    lodash escapeRegExp 转义正则特殊字符
    lodash capitalize 首字母大写
    lodash camelCase 驼峰写法
    lodash pick
    lodash random
    lodash round
    Linux 目录结构
    每天一个linux命令(6/18):lsof命令
    Linux 内核编译步骤及配置详解
  • 原文地址:https://www.cnblogs.com/kesimin/p/9547669.html
Copyright © 2011-2022 走看看