1.對list集合進行分頁
//startIndex指的是開始的小標 從0開始,pageSize是每頁記錄數inttoIndex =newInteger(startIndex)+new Integer(pageSize) ;
List listPage = voList.subList(newInteger(startIndex),toIndex>voList.size() ? voList.size():toIndex);
2.Collections對list集合按照指定屬性進行排序
//時間由新到舊進行排列 就是時間由大到小Collections.sort(voList,newComparator() {
/** * @param o1
* @param o2
* @return 返回負數表示:o1 大於o2,
* 返回0 表示:o1和o2相等,
* 返回正數表示:o1小於o2
*/ @Override
publicint compare(ProjectWorkItemAssignmentVO o1, ProjectWorkItemAssignmentVO o2) {
if(o1.getsTime().compareTo(o2.getsTime()) == 1){
return-1;
}
if(o1.getsTime().compareTo(o2.getsTime()) == 0){
return0;
}
return1;
}
});
System.out.println("排序後voList---" + voList);
//時間由舊到新進行排列 就是時間由小到大Collections.sort(voList,newComparator() {
/** * @param o1
* @param o2
* @return 返回正數表示:o1 大於o2,
* 返回0 表示:o1和o2相等,
* 返回負數表示:o1小於o2
*/ @Override
publicint compare(ProjectWorkItemAssignmentVO o1, ProjectWorkItemAssignmentVO o2) {
return o1.geteTime().compareTo(o2.geteTime());
}
});
//完成度由低至高 升序Collections.sort(voList,newComparator() {
/** * 由低到高排序
* @param o1
* @param o2
* @return 返回負數表示:o1 小於o2,
* 返回0 表示:o1和o2相等,
* 返回正數表示:o1大於o2
*/ @Override
publicint compare(ProjectWorkItemAssignmentVO o1, ProjectWorkItemAssignmentVO o2) {
if(o1.getCompletion() > o2.getCompletion()) {
return1;
}
if(o1.getCompletion() == o2.getCompletion()) {
return0;
}
return-1;
}
});
對於平常數字的排序,只需要比較大下返回對應的正數或者負數來決定按照什麼順序,對於時間日期,需要調用compareTo()方法做比較,具體可以詳看對應的api源碼
3.java獲取指定字符串,一個字符串中有多個相同的
//截取0到倒數第三個/位置之間的字符串,×××標註部分String actionRecord = "2018-09-08 14:59:42[Start時間]||2018-09-08 15:40:48[Pause時間]||";//獲取最後一個|的下標intindex = actionRecord.lastIndexOf("|");//在0-index-1範圍內,查找最後一個匹配的的字符串的位置index = actionRecord.lastIndexOf("|",index-1);//在0-index-1範圍內,查找最後一個匹配的字符串的位置index = actionRecord.lastIndexOf("|",index-1);//截取兩者之間的字符串String temp = actionRecord.substring(0,index+1);
參考連接:https://www.cnblogs.com/dujinyang/p/4788028.html
4.判斷一個時間段是否與另一個時間段重合
//判斷時間段time1~time2是否與時間段fromTime~toTime重合if(time1 >= fromTime && time1 < toTime || fromTime >= time1 && fromTime < time2){
//重合}
5.使用set對list集合去重
//利用set 對list去重複Set set =newHashSet(tempList);//使用去重複後的set 重新構建一個listList tempList2 =newArrayList(set);