1,java内存分页
import java.util.List;
public class MemoryPagination {
private Integer pageNum = 1;
private Integer pageSize = 10;
private Integer totalCount;
private Integer sizes;
private Integer pages;
private int startPos;
private int endPos;
private List<?> data;
public MemoryPagination(Integer pageNum, Integer pageSize, List<?> dataList) {
super();
this.data = dataList;
this.totalCount = dataList.size();
if (pageSize != null) {
this.pageSize = pageSize > 0 ? pageSize : 10;
}
this.pages = (int) Math.ceil((double) totalCount / this.pageSize);
if (pageNum != null) {
this.pageNum = pageNum > 0 ? ((pageNum > pages && pages > 0) ? pages : pageNum) : 1;
}
int remainingCount = this.totalCount - ((this.pageNum - 1) * this.pageSize);
this.sizes = remainingCount > this.pageSize ? this.pageSize : remainingCount;
setData();
}
public Integer getPageNum() {
return pageNum;
}
public Integer getPageSize() {
return pageSize;
}
public Integer getTotalCount() {
return totalCount;
}
public Integer getSizes() {
return sizes;
}
public Integer getPages() {
return pages;
}
public void setStartPos() {
this.startPos = (pageNum - 1) * pageSize;
}
public void setEndPos() {
this.endPos = (pageNum * pageSize) > totalCount ? totalCount : (pageNum * pageSize);
}
private void setData() {
setStartPos();
setEndPos();
this.data = data.subList(startPos, endPos);;
}
public List<?> getData() {
return data;
}
}
2,java排序
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SortUtils {
private final static Logger logger = LoggerFactory.getLogger(LogicalSort.class);
private static String field = "";
private static String order = "";
public static void mapSort(List<Map<String, String>> data, String field, String order) {
if (field == null || field.isEmpty()) {
logger.info("排序字段为空");
return;
}
if (data.size() == 0) {
logger.info("排序列表为空");
}
LogicalSort.field = field;
LogicalSort.order = order;
Collections.sort(data, new Comparator<Map>() {
@Override
public int compare(Map o1, Map o2) {
String v1 = o1.get(LogicalSort.field).toString().toLowerCase();
String v2 = o2.get(LogicalSort.field).toString().toLowerCase();
if (LogicalSort.order.equals("desc")) {
return v2.compareTo(v1);
} else {
return v1.compareTo(v2);
}
}
});
}
}