一,Map排序
①Map按key排序
Map sqrMap = Maps.newLinkedHashMap();
sqrMap = ...
List allList = Lists.newLinkedList(sqrMap.keySet());
Collects.sort(allList);
②Map按value排序(由小到大)
Map sqrMap = Maps.newLinkedHashMap();
ArrayList> allList = Lists.newArrayList(sqrMap.entrySet());
Collections.sort(allList, new Comparator>() {
public int compare(Map.Entry o1, Map.Entry o2) {
Integer v1 = o1.getValue();
Integer v2 = o2.getValue();
return v1.compareTo(v2);
}
});
③Map按value排序(由大到小)
Map sqrMap = Maps.newLinkedHashMap();
ArrayList> allList = Lists.newArrayList(sqrMap.entrySet());
Collections.sort(allList, new Comparator>() {
public int compare(Map.Entry o1, Map.Entry o2) {
Integer v1 = o1.getValue();
Integer v2 = o2.getValue();
Integer result = v2 - v1;
if(result > 0){
return 1;
}else if(result==0){
return 0;
}else{
return -1;
}
}
});
二、在统计中,经常遇到非数据库数据的统计,这里我们使用List分页,结合Map来统计。
①、在ModelView controller里面获取分页列表。
ListPageModel sqrListPage = getListPage(request,"pageNo", "pageSize");
view.addObject("sqrListPage",sqrListPage);
②、在页面代码
店铺名称
${sqr }
${sqrListPage}
③、封装的getListPage方法
@SuppressWarnings({ "unchecked", "rawtypes" })
private ListPageModel getListPage(HttpServletRequest _request,String _pageNo,String _pageSize){
int pageNo = getIntParams(_request, _pageNo, 1);
int pageSize = getIntParams(_request, _pageSize, Constant.MIN_DEFAULT_PAGE_SIZE);
ListPageModel listPage = null;
Map sqrMap = Maps.newLinkedHashMap();
sqrMap.put("可人优品", 56);
sqrMap.put("唯品衣秀", 12);
sqrMap.put("温暖衣橱", 20);
ArrayList> allList = Lists.newArrayList(sqrMap.entrySet());
Collections.sort(allList, new Comparator>() {
public int compare(Map.Entry o1, Map.Entry o2) {
Integer v1 = o1.getValue();
Integer v2 = o2.getValue();
Integer result = v2 - v1;
if(result > 0){
return 1;
}else if(result==0){
return 0;
}else{
return -1;
}
}
});
listPage = new ListPageModel(allList,pageSize);
List sqrList = listPage.getObjects(pageNo);
listPage.setList(sqrList);
return listPage;
}
④分页工具类
public class ListPageModel {
private int currPageNo = 1; // 当前页
private int totalPages = 0; // 总页数
private int pageSize = Constant.MIN_DEFAULT_PAGE_SIZE;
private int totalRows = 0; // 总数据数
private int pageStartRow = 0;// 每页的起始数
private int pageEndRow = 0; // 每页显示数据的终止数
private boolean hasNextPage = false; // 是否有下一页
private boolean hasPreviousPage = false; // 是否有前一页
private List list;
public ListPageModel(List list, int pageSize) {
init(list, pageSize);// 通过对象集,记录总数划分
}
/**
* 初始化list,并告之该list每页的记录数
*
* @param list
* @param pageSize
*/
public void init(List list, int pageSize) {
this.pageSize = pageSize;
this.list = list;
totalRows = list.size();
hasPreviousPage = false;
if ((totalRows % pageSize) == 0) {
totalPages = totalRows / pageSize;
} else {
totalPages = totalRows / pageSize + 1;
}
if (currPageNo >= totalPages) {
hasNextPage = false;
} else {
hasNextPage = true;
}
if (totalRows < pageSize) {
this.pageStartRow = 0;
this.pageEndRow = totalRows;
} else {
this.pageStartRow = 0;
this.pageEndRow = pageSize;
}
}
/**
* 获取第几页的内容
*
* @param page
* @return
*/
public List getObjects(int page) {
if (page == 0){
this.setCurrPageNo(1);
}else{
this.setCurrPageNo(page);
}
this.disposePage();
if (page * pageSize < totalRows) {// 判断是否为最后一页
pageEndRow = page * pageSize;
pageStartRow = pageEndRow - pageSize;
} else {
pageEndRow = totalRows;
pageStartRow = pageSize * (totalPages - 1);
}
List objects = null;
if (!list.isEmpty()) {
objects = list.subList(pageStartRow, pageEndRow);
}
return objects;
}
/**
* 处理分页
*/
private void disposePage() {
if (currPageNo == 0) {
currPageNo = 1;
}
if ((currPageNo - 1) > 0) {
hasPreviousPage = true;
} else {
hasPreviousPage = false;
}
if (currPageNo >= totalPages) {
hasNextPage = false;
} else {
hasNextPage = true;
}
}
/**
* 获取下一页
* @return
*/
public List getNextPage() {
currPageNo = currPageNo + 1;
disposePage();
return getObjects(currPageNo);
}
/**
* 获取上一页
* @return
*/
public List getPreviousPage() {
currPageNo = currPageNo - 1;
if ((currPageNo - 1) > 0) {
hasPreviousPage = true;
} else {
hasPreviousPage = false;
}
if (currPageNo >= totalPages) {
hasNextPage = false;
} else {
hasNextPage = true;
}
return getObjects(currPageNo);
}
// public void description() {
// String description = "共有数据数:" + this.getTotalRows() +
// "共有页数: " + this.getTotalPages() +
// "当前页数为:" + this.getPage() +
// " 是否有前一页: " + this.isHasPreviousPage() +
// " 是否有下一页:" + this.isHasNextPage() +
// " 开始行数:" + this.getPageStartRow() +
// " 终止行数:" + this.getPageEndRow();
// System.out.println(description);
// }
/**
* 判断是否有下一页
* @return
*/
public boolean isNext() {
return list.size() > 5;
}
/**
* 获取首页
* @return
*/
public List getFistPage() {
if (this.isNext()) {
return list.subList(0, pageSize);
} else {
return list;
}
}
public boolean isHasNextPage() {
return hasNextPage;
}
public boolean isHasPreviousPage() {
return hasPreviousPage;
}
//=====================================getter or setter===============================================
public void setHasPreviousPage(boolean hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public int getCurrPageNo() {
return currPageNo;
}
public void setCurrPageNo(int currPageNo) {
this.currPageNo = currPageNo;
}
public int getPageEndRow() {
return pageEndRow;
}
public void setPageEndRow(int pageEndRow) {
this.pageEndRow = pageEndRow;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageStartRow() {
return pageStartRow;
}
public void setPageStartRow(int pageStartRow) {
this.pageStartRow = pageStartRow;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public int getTotalRows() {
return totalRows;
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
//================================分页=================================
private String funcName = "listpage";
public String getFuncName() {
return funcName;
}
public void setFuncName(String funcName) {
this.funcName = funcName;
}
/**
* 默认输出当前分页标签 ${page}
*/
@Override
public String toString() {
String div1="" + currPageNo + "/" + totalPages + "\n";
StringBuilder div2=new StringBuilder();
div2.append("\n\n\n");
StringBuilder sb = new StringBuilder();
sb.append(div1).append(div2);
sb.insert(0,"\n").append("\n");
return sb.toString();
}
}
欢迎来我的小店看看》》》《可人优品》》》
原文地址:http://vipshow.iteye.com/blog/2038026