Map按value排序(由大到小-由小到大),对List列表分页

 

一,Map排序

①Map按key排序

 

Map<String, Integer> sqrMap = Maps.newLinkedHashMap();
sqrMap = ...
List<String> allList = Lists.newLinkedList(sqrMap.keySet());
Collects.sort(allList);

 

②Map按value排序(由小到大)

 

 

Map<String, Integer> sqrMap = Maps.newLinkedHashMap();
ArrayList<Entry<String,Integer>> allList = Lists.newArrayList(sqrMap.entrySet());    
Collections.sort(allList, new Comparator<Map.Entry<String, Integer>>() {    
	 public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {    
	           Integer v1 = o1.getValue();
	           Integer v2 = o2.getValue();
	           return v1.compareTo(v2);  
	 } 
});  

 

 

③Map按value排序(由大到小)

 

Map<String, Integer> sqrMap = Maps.newLinkedHashMap();
ArrayList<Entry<String,Integer>> allList = Lists.newArrayList(sqrMap.entrySet());    
 Collections.sort(allList, new Comparator<Map.Entry<String, Integer>>() {    
	 public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> 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<String> sqrListPage = getListPage(request,"pageNo", "pageSize");
view.addObject("sqrListPage",sqrListPage);

 

②、在页面代码

 

 

<table id="sqrTable" width="100%" cellpadding="0" cellspacing="0">
<tr class="title-1"><td>店铺名称</td></tr>
<c:forEach var="sqr" items="${sqrListPage.list }">
<tr><td>${sqr }</td></tr>
</c:forEach>
</table>
<div class="list-page">${sqrListPage}</div>

 

③、封装的getListPage方法

 

@SuppressWarnings({ "unchecked", "rawtypes" })
	private ListPageModel<String> getListPage(HttpServletRequest _request,String _pageNo,String _pageSize){
		int pageNo =  getIntParams(_request, _pageNo, 1);
		int pageSize = getIntParams(_request, _pageSize, Constant.MIN_DEFAULT_PAGE_SIZE);
		ListPageModel<String> listPage = null;
		Map<String, Integer> sqrMap = Maps.newLinkedHashMap();
		sqrMap.put("可人优品", 56);
		sqrMap.put("唯品衣秀", 12);
		sqrMap.put("温暖衣橱", 20);
		ArrayList<Entry<String,Integer>> allList = Lists.newArrayList(sqrMap.entrySet());    
        Collections.sort(allList, new Comparator<Map.Entry<String, Integer>>() {    
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> 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<String> sqrList = listPage.getObjects(pageNo);
        listPage.setList(sqrList);
		return listPage;
	}

 

 ④分页工具类

public class ListPageModel<T> {
	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<T> list;

	public ListPageModel(List<T> list, int pageSize) {
		init(list, pageSize);// 通过对象集,记录总数划分
	}

	/**
	 * 初始化list,并告之该list每页的记录数
	 * 
	 * @param list
	 * @param pageSize
	 */
	public void init(List<T> 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<T> 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<T> 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<T> getNextPage() {
		currPageNo = currPageNo + 1;
		disposePage();
		return getObjects(currPageNo);
	}

	
	/**
	 * 获取上一页
	 * @return
	 */
	public List<T> 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<T> 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<T> getList() {
		return list;
	}

	public void setList(List<T> 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;
	}
	/**
	 * 默认输出当前分页标签 <div class="page">${page}</div>
	 */
	@Override
	public String toString() {
		String div1="<div class=\"total-pages fl\">" + currPageNo  + "/" + totalPages + "</div>\n";
		StringBuilder div2=new StringBuilder();
		div2.append("<div class=\"pagination\">\n<ul>\n");
		if (currPageNo==1) {// 如果是首页
			div2.append("<li class=\"pglink pre\"><a href=\"javascript:\" title=\"上一页\" class=\"disabled\"><span><i></i></span></a></li>\n");
		} else {
			div2.append("<li class=\"pglink pre\"><a href=\"javascript:" + getFuncName() + "(" + (currPageNo - 1) + ","+ pageSize + ");\" title=\"上一页\"><span><i></i></span></a></li>\n");
		}
		if (currPageNo == totalPages) {
			div2.append("<li class=\"pglink next\"><a href=\"javascript:\" title=\"下一页\" class=\"disabled\"><span><i></i></span></a></li>\n");
		} else {
			div2.append("<li class=\"pglink next\"><a href=\"javascript:" + getFuncName() + "(" + (currPageNo + 1) + "," + pageSize + ");\" title=\"下一页\">" + "<span><i></i></span></a></li>\n");
		}
		div2.append("<li class=\"skippage\">第<input type=\"text\" id=\"rePageNo\" value=\""+currPageNo+"\" class=\"input-mini\"/>页</li>\n");
		div2.append("<li class=\"\"><a title=\"跳转\" onclick=\"javascript:" + getFuncName() + "($(this).parent().parent().find('#rePageNo').val()," + pageSize + ");\" target=\"_self\"/><span>GO</span></a></li>\n");
		div2.append("</ul>\n</div>\n");
		
		StringBuilder sb = new StringBuilder();
		sb.append(div1).append(div2);
		sb.insert(0,"<div class=\"pager original-state\">\n").append("</div>\n");
		return sb.toString();
	}
}

 

 

欢迎来我的小店看看》》》《可人优品》》》

原文地址:http://vipshow.iteye.com/blog/2038026

 

你可能感兴趣的:(list分页,Map排序)