<span style="font-size:14px;">/**
* 数据分组 作用项目: 表情分组时数据拆分
*
* @author syusikoku
* @date 2016-8-17
*/
public class DataSplitGroup2 {
private static ArrayList<String> sourceList;
/**
* 1 -- 60
*/
private static int allMaxDatas = 37;
// 每组20条
private static int maxCount = 20;
private static Map<Integer, List<String>> cacheGroupList;
private static int startIndex;
private static int endIndex;
public static void main(String[] args) {
sourceList = new ArrayList<String>();
initDatas();
splitDatas();
printMsg();
}
private static void printMsg() {
Set<Entry<Integer, List<String>>> entrySet =
cacheGroupList.entrySet();
for (Entry<Integer, List<String>> entry : entrySet) {
System.out.println(
"pos:" + entry.getKey() + ",list:" + entry.getValue());
}
}
private static void splitDatas() {
// TODO Auto-generated method stub
// 每组20条
int latMaxSize = sourceList.size() / maxCount;
cacheGroupList = new HashMap<Integer, List<String>>();
if (sourceList.size() % maxCount == 0) {
for (int i = 0; i < latMaxSize; i++) {
startIndex = i * maxCount;
endIndex = (i * maxCount) + maxCount;
cacheGroupList.put(i,
sourceList.subList(startIndex, endIndex));
}
} else {
// 能够整除的数据
for (int i = 0; i < latMaxSize; i++) {
startIndex = i * maxCount;
endIndex = (i * maxCount) + maxCount;
cacheGroupList.put(i,
sourceList.subList(startIndex, endIndex));
}
// 需要新增加一页
// 最后一页的数据
cacheGroupList.put(cacheGroupList.size(),
sourceList.subList(endIndex, sourceList.size()));
}
}
private static void initDatas() {
for (int i = 0; i < allMaxDatas; i++) {
sourceList.add("itcast :" + i);
}
}
}</span>
数组分组代码模拟
最新推荐文章于 2023-08-20 23:25:59 发布