Java:List分组

目前List分组主要运用数据分组线程分批

1、分组公共类 

public class ListsUtils {

	/**
	 * 将一组数据平均分成n组
	 *
	 * @param source 要分组的数据源
	 * @param n      平均分成n组
	 * @param 
	 * @return
	 */
	public static  List> averageAssign(List source, int n) {
		List> result = new ArrayList<>();
		int remainder = source.size() % n;  //(先计算出余数)
		int number = source.size() / n;  //然后是商
		int offset = 0;//偏移量
		for (int i = 0; i < n; i++) {
			List value = null;
			if (remainder > 0) {
				value = source.subList(i * number + offset, (i + 1) * number + offset + 1);
				remainder--;
				offset++;
			} else {
				value = source.subList(i * number + offset, (i + 1) * number + offset);
			}
			result.add(value);
		}
		return result;
	}

	/**
	 * 将一组数据固定分组,每组n个元素
	 *
	 * @param source 要分组的数据源
	 * @param n      每组n个元素
	 * @param 
	 * @return
	 */
	public static  List> fixedGrouping(List source, int n) {
		if (null == source || source.size() == 0 || n <= 0)
			return null;
		List> result = new ArrayList<>();
		int sourceSize = source.size();
		int size = (source.size() / n) + 1;
		for (int i = 0; i < size; i++) {
			List subset = new ArrayList();
			for (int j = i * n; j < (i + 1) * n; j++) {
				if (j < sourceSize) {
					subset.add(source.get(j));
				}
			}
			result.add(subset);
		}
		return result;
	}


	/**
	 * 将一组数据固定分组,每组n个元素
	 *
	 * @param source 要分组的数据源
	 * @param n      每组n个元素
	 * @param 
	 * @return
	 */
	public static  List> fixedGrouping2(List source, int n) {
		if (null == source || source.size() == 0 || n <= 0)
			return null;
		List> result = new ArrayList<>();
		int remainder = source.size() % n;
		int size = (source.size() / n);
		for (int i = 0; i < size; i++) {
			List subset = source.subList(i * n, (i + 1) * n);
			result.add(subset);
		}
		if (remainder > 0) {
			List subset = source.subList(size * n, size * n + remainder);
			result.add(subset);
		}
		return result;
	}

	/**
	 * 指定数字转换为集合
	 *
	 * @param number
	 * @return
	 */
	public static List fixedList(int number) {
		List list = Lists.newArrayList();
		for (int i = 1; i <= number; i++) {
			list.add(i);
		}
		return list;
	}

	/**
	 * 指定数字转换为集合
	 * @param minInt
	 * @param maxInt
	 * @return
	 */
	public static List fixedMinMaxList(int minInt, int maxInt) {
		List list = Lists.newArrayList();
		for (int i = minInt; i <= maxInt; i++) {
			list.add(i);
		}
		return list;
	}

	/**
	 * 二维集合
	 *
	 * @param number
	 * @param number2
	 * @return
	 */
	public static List fixedList(int number, int number2) {
		List list = Lists.newArrayList();
		for (int i = 1; i <= number; i++) {
			for (int k = 1; k <= number2; k++) {
				list.add(i + "-" + k);
			}
		}
		return list;
	}

	/**
	 * 获取时间分组
	 *
	 * @return
	 */
	public static Map getTimesGroup() {
		Map map = Maps.newHashMap();
		int table = 10;
		int year = 1990;
		for (int i = 1; i <= table; i++) {
			if (i > 1 && i <= 8) {
				for (int k = 0; k < 3; k++) {
					map.put(year++, 100 + i);
				}
			} else if (i > 8) {
				for (int k = 0; k < 2; k++) {
					if (i > 9) {
						map.put(year++, 1000 + i);
					} else {
						map.put(year++, 100 + i);
					}
				}
			} else {
				for (int k = 0; k < 10; k++) {
					map.put(year++, 100 + i);
				}
			}
		}
		return map;
	}

	/**
	 * 根据已有数据进行数字分组
	 * @param list
	 * @return
	 */
	public static Map getNumberGroup(List list) {
		Map map = Maps.newHashMap();
		list = list.stream().distinct().collect(Collectors.toList());
		Collections.sort(list);
		List list2 = Lists.newArrayList();
		List list3 = Lists.newArrayList();
		for (int i = 0; i < list.size(); i++) {
			StringBuffer key = new StringBuffer();
			if (i % 2 == 0) {
				list2.add(list.get(i));
			} else {
				list3.add(list.get(i));
			}
		}
		if (list.size() % 2 != 0) {
			list3.add(Collections.max(list));
		}
		for (int i = 0; i < list2.size(); i++) {
			map.put(list2.get(i) + "~" + list3.get(i), 0);
		}
		return map;
	}

	/**
	 * 获取多个list最大的size
	 * @param lists
	 * @return
	 */
	public static Integer getMaxSize(List... lists) {
		if (ObjectUtils.isEmpty(lists)) {
			return 0;
		}
		List maxSizeList = new ArrayList<>();
		for (List list : lists) {
			maxSizeList.add(ObjectUtils.isEmpty(list) ? 0 : list.size());
		}
		return ObjectUtils.isEmpty(maxSizeList) ? 0 : Collections.max(maxSizeList);
	}

	/**
	 * 填充空值
	 * @param list
	 * @param maxSize
	 * @param 
	 * @return
	 */
	public static  List fillValue(List list, int maxSize) {
		if(list.size() < maxSize){
			for (int i = 0; i < maxSize-list.size() +1; i++) {
				list.add(null);
			}
		}
		return list;
	}

}

2、关键代码

private final int MAX_GROUP_NUMBER = 15;
private final int MAX_THREAD_POOL_NUMBER = 150;
private final ExecutorService executorService = Executors.newFixedThreadPool(MAX_THREAD_POOL_NUMBER);

// 获取页数-向上取整
Double num = Math.ceil(data.getDouble("total") / 20);
int pageNumber = num.intValue();
List integerList = ListsUtils.fixedList(pageNumber);
List> groupList = ListsUtils.fixedGrouping(integerList, MAX_GROUP_NUMBER);
CountDownLatch countDownLatch = new CountDownLatch(groupList.size());


groupList.forEach(list->{
    executorService.submit(() -> {
        list.forEach(i-> {
			try {
				// 关键代码..............
				Thread.sleep(200);
			} catch (Exception ex) {
				log.error("异常 >> " + ex.getMessage());
				ex.printStackTrace();
			} finally {
		    }
			log.info("第" + i + "页列表初始化");
		});
    countDownLatch.countDown();
});
try {
    // 调整频次
	Thread.sleep(1500);
} catch (InterruptedException e) { }
});
executorService.shutdown();
countDownLatch.await();
log.info("saveOrUpdateStock >> 所有的子线程都结束了!");

你可能感兴趣的:(Java,java,list)