ListUtil常用操作
发布日期:2021-08-21 13:17:40 浏览次数:35 分类:技术文章

本文共 2784 字,大约阅读时间需要 9 分钟。

/**     * 获取列表总页数     */    public static 
int getListPages(List
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
List
subListByPage(List
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
List
subListByPosition(List
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
void sortByField(List
dtoList,String fieldName,String order) { int compare=1; if ("desc".equals(order)){ compare=-1; } int finalCompare = compare; Collections.sort(dtoList, new Comparator
() { @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

转载地址:https://blog.csdn.net/weixin_33720078/article/details/93642001 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Spark:windows下配置spark开发环境
下一篇:SecureCRT恢复默认字体

发表评论

最新留言

不错!
[***.144.177.141]2024年04月20日 10时54分01秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章