大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE稳定放心使用
java分页与排序
代码 如下不解释
public class SortOb implements Comparable{
private String name;
private int age;
private Date createDate;
public SortOb(String name,int age,Date createDate) {
this.name = name;
this.age = age;
this.createDate = createDate;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
// @Override
// public int compareTo(SortOb o) {
// return this.age – o.getAge();
// }
@Override
public int compareTo(SortOb o) {
try {
Date dt1 = o.getCreateDate();
Date dt2 = this.getCreateDate();
if (dt1.getTime() > dt2.getTime()) {
return -1;//小的放前面
}else {
return 1;
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}
public class ListSortTest {
public static void main(String[] args) throws ParseException {
List<SortOb> list = new ArrayList<SortOb>();
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dateA = format1.parse("2002-1-18 22:01:01");
Date dateB = format1.parse("2001-1-18 22:01:01");
Date dateC = format1.parse("2005-1-18 22:01:01");
Date dateD = format1.parse("2004-1-18 22:01:01");
Date dateE = format1.parse("2006-1-18 22:01:01");
Date dateF = format1.parse("2003-1-18 22:01:01");
Date dateG = format1.parse("2007-1-18 22:01:01");
Date dateH = format1.parse("2009-1-18 22:01:01");
Date dateI = format1.parse("2006-1-18 22:01:01");
Date dateJ = format1.parse("2008-1-18 22:01:01");
Date dateK = format1.parse("2010-1-18 22:01:01");
Date dateL = format1.parse("2009-1-18 22:01:01");
SortOb a = new SortOb("bb", 2,dateA);
SortOb b = new SortOb("aa", 1,dateB);
SortOb c = new SortOb("ee", 5,dateC);
SortOb d = new SortOb("dd", 4,dateD);
SortOb e = new SortOb("ff", 6,dateE);
SortOb f = new SortOb("cc", 3,dateF);
SortOb g = new SortOb("gg", 7,dateG);
SortOb h = new SortOb("ii", 9,dateH);
SortOb i = new SortOb("zz", 6,dateI);
SortOb j = new SortOb("hh", 8,dateJ);
SortOb k = new SortOb("hh", 10,dateK);
SortOb l = new SortOb("jj", 9,dateL);
list.add(a);
list.add(b);
list.add(c);
list.add(d);
list.add(e);
list.add(f);
list.add(g);
list.add(h);
list.add(i);
list.add(j);
list.add(k);
list.add(l);
Collections.sort(list, (SortOb b1, SortOb b2) -> b2.compareTo(b1));
Map<String, Object> map = getPagingResultMap(list,2,10);
List<SortOb> pageList = (List<SortOb>) map.get("result");
for (SortOb ob : pageList) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(ob.getCreateDate());
System.out.println(dateString);
}
// for (SortOb ob : list) {
// System.out.println(ob.getAge());
// }
}
public static Map<String, Object> getPagingResultMap(List<SortOb> list, Integer currPageNo, Integer pageSize) {
Map<String, Object> retMap = new HashMap<>();
if (list.isEmpty()) {
retMap.put("result", Collections.emptyList());
retMap.put("totalPageNum", 0);
return retMap;
}
int totalRowNum = list.size();
//页数超过数据总页数返回空list
if (totalRowNum< pageSize * (currPageNo-1)) {
retMap.put("result", Collections.emptyList());
retMap.put("totalPageNum", totalRowNum);
return retMap;
}
int totalPageNum = (totalRowNum - 1) / pageSize + 1;
int realPageNo = currPageNo;
if (currPageNo > totalPageNum) {
realPageNo = totalPageNum;
} else if (currPageNo < 1) {
realPageNo = 1;
}
int fromIdx = (realPageNo - 1) * pageSize;
int toIdx = realPageNo * pageSize > totalRowNum ? totalRowNum : realPageNo * pageSize;
List<SortOb> result = list.subList(fromIdx, toIdx);
retMap.put("result", result);
retMap.put("totalPageNum", totalPageNum);
return retMap;
}
// public static void main(String[] args) {
// int totalRowNum = 12;
// int pageSize = 10;
// int currPageNo = 2;
// System.out.println((totalRowNum< pageSize * (currPageNo-1)));
// }
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/186497.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...