标签:
长期更新,主要记录List的各种常用操作整理。
对List进行排序
// Collections.sort(重写toString()进行排序区分)
List<ObjectName> list =new ArrayList<ObjectName>();Collections.sort(list, new Comparator<ObjectName>(){publicint compare(ObjectName o1, ObjectName o2){return o1.toString().compareTo(o2.toString());}});
|
// Arrays.sort
ObjectName[] arr =new ObjectName[10];Arrays.sort(arr, new Comparator<ObjectName>(){publicint compare(ObjectName o1, ObjectName o2){return o1.toString().compareTo(o2.toString());}});
|
对List进行分页
/**
*
* @param pageNo
* 当前页码
* @param pageSize
* 页数
* @param vouchers
* 所有集合
* @return
* @throws Exception
*/
private List<Voucher> getPage(int pageNo, int pageSize, List<Voucher> vouchers) {
List<Voucher> result = new ArrayList<Voucher>();
if (CollectionUtils.isNotEmpty(vouchers)) {
int allCount = vouchers.size();
int pageCount = (allCount + pageSize - 1) / pageSize;
if (pageNo >= pageCount) {
pageNo = pageCount;
}
int start = (pageNo - 1) * pageSize;
int end = pageNo * pageSize;
if (end >= allCount) {
end = allCount;
}
for (int i = start; i < end; i++) {
result.add(vouchers.get(i));
}
}
return (CollectionUtils.isNotEmpty(vouchers)) ? result : Collections.emptyList();
}
(感觉有更好的实现方案)
List常用整理
标签:
原文地址:http://blog.csdn.net/jerome_s/article/details/45584387