將百萬數據分組取出

需求

將百萬數據分組取出

思路:

將假設只有1000數據, 如何取出?
將數據放入Map當中, new HashMap<String, List>

代碼

//模擬1000數據
List<String> list = new ArrayList<>();
for( int i = 0; i < 1000; i ++){
    list.add(i+"Str");
}

int size = list.size();
//設置分組後的每組數量
int toIndex= 100// 用Map來存儲數據,設置key
Map<String,List> map = new HashMap<>();
int key = 0//獲取每組的value
for(int i = 0; i < size; i+=toIndex){
	List<String> newList = new ArrayList<>();
	//如何截取新的newList
	if(i + toIndex > size){
		toIndex = size - i;
	}
	newList = list.subList(i, i+toIndex);
	map.put("key" + key, newList);
	key++;
}

//遍歷map
map.foreach((key,value) ->{
	System.out.println("key =" + key + ": " + value);
})
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章