日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java把一个list_java 把一个大list分成N个小list,然后用map存储的小方法

發布時間:2024/9/19 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java把一个list_java 把一个大list分成N个小list,然后用map存储的小方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

有時候一個很大的list集合數據,萬,百萬級別的數據,但怕數據量過大,無法處理,可以分成N個小list然后分片處理數據。可以結合多線程來提高性能。

代碼:

package cn.uc.db.report.utils;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

/**

*

* 轉換工具

*

* @date 2016/12/7

* @author ruiliang

* @version 1.0.0

*

*/

public class ListToMapUtil {

/**

* 把集合分片到指定槽位的map上

*

* @param list

* @param slotPosition

* map大小->槽位

* @return

*/

public static Map> shardingToMap(List list, int slotPosition) {

// 返回數據集合

Map> hmap = new HashMap>();

int list_size = list.size();

int thread_exec_count = slotPosition;// 每個線程分的數據數

int thread_num = 1;// 根據數據量 求出線程數

int list_remainder_num = 0;// 集合余數

// 先把list集合進行分片處理

if (list_size > thread_exec_count) {

thread_num = list_size / thread_exec_count;

if (list_size % thread_exec_count > 0) {// 有余數

// 求集合余數

list_remainder_num = thread_num * thread_exec_count;

thread_num++;

}

}

if (thread_num == 1) {

// 如果1個槽位可以裝下,直接返回一個

hmap.put(1, list);

}

// ~~~~~~~~~~~~~~~~~~~如果數據需要多線程處理~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// 根據j,來反推key

int key = 0;

int j = 1;

List temp = new ArrayList<>();

for (T t : list) {

temp.add(t);

// 每過一個線程,K就變+1,根據線程來歸納數據

if (j >= thread_exec_count) {

// System.out.println("temp:" + temp);

hmap.put(key, temp);

temp = new ArrayList<>();

j = 0;

key++;// key++ 相當于在化分大集合

}

j++;

}

// 余數加入map

hmap.put(-1, list.subList(list_remainder_num, list_size));

return hmap;

}

public static void main(String[] args) {

List accounts = new ArrayList<>();

for (long i = 1; i < 103; i++) {

accounts.add(i);

}

System.out.println(accounts);

Map> maps = ListToMapUtil.shardingToMap(accounts, 5);

System.out.println(maps);

}

/**

* oputput:

*

* [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,

* 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,

* 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,

* 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,

* 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92,

* 93, 94, 95, 96, 97, 98, 99, 100, 101, 102]

*

* {0=[1, 2, 3, 4, 5], 1=[6, 7, 8, 9, 10], 2=[11, 12, 13, 14, 15], 3=[16,

* 17, 18, 19, 20], 4=[21, 22, 23, 24, 25], 5=[26, 27, 28, 29, 30], 6=[31,

* 32, 33, 34, 35], 7=[36, 37, 38, 39, 40], 8=[41, 42, 43, 44, 45], 9=[46,

* 47, 48, 49, 50], 10=[51, 52, 53, 54, 55], 11=[56, 57, 58, 59, 60],

* 12=[61, 62, 63, 64, 65], 13=[66, 67, 68, 69, 70], 14=[71, 72, 73, 74,

* 75], 15=[76, 77, 78, 79, 80], 17=[86, 87, 88, 89, 90], 16=[81, 82, 83,

* 84, 85], 19=[96, 97, 98, 99, 100], 18=[91, 92, 93, 94, 95], -1=[101,

* 102]}

*

*

*/

}

可以結合多線和,參考多線程代碼

總結

以上是生活随笔為你收集整理的java把一个list_java 把一个大list分成N个小list,然后用map存储的小方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。