Java Code Examples for org.apache.commons.collections4.ListUtils#partition()

The following examples show how to use org.apache.commons.collections4.ListUtils#partition() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UserExcelTest.java    From fw-spring-cloud with Apache License 2.0 6 votes vote down vote up
/**
 * 写入到不同的sheet
 */
@Test
public void testWriteMethodMoreSheet() {
    // 方法3 如果写到不同的sheet 不同的对象
    String fileName = TestFileUtil.getPath() + System.currentTimeMillis() + "user.xlsx";
    // 这里 指定文件
    ExcelWriter excelWriter = EasyExcel.write(fileName).build();
    List<SysUser> sysUsers = initDate();
    List<List<SysUser>> partition = ListUtils.partition(sysUsers, 5);//分成多份,没份5条数据
    // 去调用写入,这里我调用了多次,这里最终会写到多个sheet里面
    for (int i = 0; i < partition.size(); i++) {
        // 每次都要创建writeSheet 这里注意必须指定sheetNo 而且sheetName必须不一样。这里注意DemoData.class 可以每次都变,我这里为了方便 所以用的同一个class 实际上可以一直变
        WriteSheet writeSheet = EasyExcel.writerSheet(i, "模板" + i).head(SysUser.class).build();
        // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
        List<SysUser> data = partition.get(i);
        excelWriter.write(data, writeSheet);
    }
    /// 千万别忘记finish 会帮忙关闭流
    excelWriter.finish();
}
 
Example 2
Source File: ResultBuilder.java    From feilong-taglib with Apache License 2.0 6 votes vote down vote up
/**
 * 拼接.
 *
 * @param itemSrcList
 *            the item src list
 * @param template
 *            the template
 * @param standardHttpConcatParam
 *            the standard http concat param
 * @return the string
 * @since 1.11.1
 */
private static String handlerConcat(List<String> itemSrcList,String template,HttpConcatParam standardHttpConcatParam){
    Integer autoPartitionSize = GLOBAL_CONFIG.getAutoPartitionSize();
    //不需要分片
    if (null == autoPartitionSize || itemSrcList.size() <= autoPartitionSize){
        return MessageFormatUtil.format(template, ConcatLinkResolver.resolver(itemSrcList, standardHttpConcatParam));
    }

    //---------------------------------------------------------------
    //since 1.12.6
    //将 list 分成 N 份
    List<List<String>> groupList = ListUtils.partition(itemSrcList, autoPartitionSize);

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < groupList.size(); ++i){
        sb.append("<!-- HttpConcatTag,auto partition [" + (i + 1) + "] -->");
        sb.append(lineSeparator());

        sb.append(MessageFormatUtil.format(template, ConcatLinkResolver.resolver(groupList.get(i), standardHttpConcatParam)));
        sb.append(lineSeparator());
    }
    return sb.toString();
}
 
Example 3
Source File: TaskResultCreator.java    From scheduling with GNU Affero General Public License v3.0 6 votes vote down vote up
private Map<String, byte[]> extractTaskResultsAndMergeIntoMap(SchedulerDBManager dbManager,
        EligibleTaskDescriptor eligibleTaskDescriptor, InternalJob job) {
    Map<String, byte[]> mergedVariables = new HashMap<>();

    int numberOfParentTasks = eligibleTaskDescriptor.getParents().size();
    List<TaskId> parentIds = new ArrayList<>(numberOfParentTasks);
    for (int i = 0; i < numberOfParentTasks; i++) {
        parentIds.add(eligibleTaskDescriptor.getParents().get(i).getTaskId());
    }

    // Batch fetching of parent tasks results
    Map<TaskId, TaskResult> taskResults = new HashMap<>();
    for (List<TaskId> parentsSubList : ListUtils.partition(new ArrayList<>(parentIds),
                                                           PASchedulerProperties.SCHEDULER_DB_FETCH_TASK_RESULTS_BATCH_SIZE.getValueAsInt())) {
        taskResults.putAll(dbManager.loadTasksResults(job.getId(), parentsSubList));
    }

    for (TaskResult taskResult : taskResults.values()) {
        if (taskResult.getPropagatedVariables() != null) {
            mergedVariables.putAll(taskResult.getPropagatedVariables());
        }
    }
    return mergedVariables;
}
 
Example 4
Source File: GetFilesTask.java    From Amphitheatre with Apache License 2.0 6 votes vote down vote up
@Override
protected void onPostExecute(List<SmbFile> files) {
    try {
        final int cpuCount = Runtime.getRuntime().availableProcessors();
        final int maxPoolSize = cpuCount * 2 + 1;
        final int partitionSize = files.size() < maxPoolSize ? files.size() : (files.size() / maxPoolSize);

        List<List<SmbFile>> subSets = ListUtils.partition(files, partitionSize);

        mNumOfSets = subSets.size();

        for (List<SmbFile> subSet : subSets) {
            if (mIsMovie) {
                new DownloadMovieTask(mContext, mConfig, subSet, this)
                        .executeOnExecutor(THREAD_POOL_EXECUTOR);
            } else {
                new DownloadTvShowTask(mContext, mConfig, subSet, this)
                        .executeOnExecutor(THREAD_POOL_EXECUTOR);
            }
        }
    } catch (Exception e) {
        if (mCallback != null) {
            mCallback.failure();
        }
    }
}
 
Example 5
Source File: DefaultPartitionThreadExecutor.java    From feilong-core with Apache License 2.0 5 votes vote down vote up
/**
 * Builds the thread array.
 * 
 * <p>
 * 调用 {@link ListUtils#partition(List, int)} 对list 分成N份,对应的创建N份线程,每个线程的 名字 参见 {@link #buildThreadName(int, PartitionRunnableBuilder)}
 * </p>
 * 
 * <p>
 * 会自动创建 ThreadGroup,线程组名字参见 {@link #buildThreadGroupName(List, PartitionRunnableBuilder)}, <br>
 * 所有新建的线程将归属到该 线程组,你可以在自定义的partitionRunnableBuilder中监控或者管理 该ThreadGroup
 * </p>
 *
 * @param <T>
 *            the generic type
 * @param list
 *            the list
 * @param eachSize
 *            the per size
 * @param paramsMap
 *            the params map
 * @param partitionRunnableBuilder
 *            the group runnable builder
 * @return the thread[]
 */
private static <T> Thread[] buildThreadArray(
                List<T> list,
                int eachSize,
                Map<String, ?> paramsMap,
                PartitionRunnableBuilder<T> partitionRunnableBuilder){

    //使用group进行管理  
    ThreadGroup threadGroup = new ThreadGroup(buildThreadGroupName(list, partitionRunnableBuilder));

    //将 list 分成 N 份
    List<List<T>> groupList = ListUtils.partition(list, eachSize);

    //-------------------------------------------------------------------
    int i = 0;
    Thread[] threads = new Thread[groupList.size()];
    for (List<T> perBatchList : groupList){
        String threadName = buildThreadName(i, partitionRunnableBuilder);

        PartitionThreadEntity partitionThreadEntity = new PartitionThreadEntity(
                        threadName,
                        list.size(),
                        eachSize,
                        i,
                        perBatchList.size());

        Runnable runnable = partitionRunnableBuilder.build(perBatchList, partitionThreadEntity, paramsMap);
        threads[i] = new Thread(threadGroup, runnable, threadName);
        i++;
    }

    //---------------------------------------------------------------

    LOGGER.info("total list size:[{}],build [{}] threads,perSize:[{}]", list.size(), threads.length, eachSize);
    return threads;
}
 
Example 6
Source File: CollectionApachePartitionUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public final void givenList_whenParitioningIntoNSublists_thenCorrect() {
    final List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);

    final List<List<Integer>> subSets = ListUtils.partition(intList, 3);

    // When
    final List<Integer> lastPartition = subSets.get(2);
    final List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8);
    assertThat(subSets.size(), equalTo(3));
    assertThat(lastPartition, equalTo(expectedLastPartition));
}
 
Example 7
Source File: CollectionApachePartitionUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public final void givenListPartitioned_whenOriginalListIsModified_thenPartitionsChange() {
    // Given
    final List<Integer> intList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
    final List<List<Integer>> subSets = ListUtils.partition(intList, 3);

    // When
    intList.add(9);
    final List<Integer> lastPartition = subSets.get(2);
    final List<Integer> expectedLastPartition = Lists.<Integer> newArrayList(7, 8, 9);
    assertThat(lastPartition, equalTo(expectedLastPartition));
}
 
Example 8
Source File: CloudwatchReporter.java    From bender with Apache License 2.0 4 votes vote down vote up
@Override
public void write(ArrayList<Stat> stats, long invokeTimeMs, Set<Tag> tags) {
  Date dt = new Date();
  dt.setTime(invokeTimeMs);

  Collection<Dimension> parentDims = tagsToDimensions(tags);
  List<MetricDatum> metrics = new ArrayList<MetricDatum>();

  /*
   * Create CW metric objects from bender internal Stat objects
   */
  for (Stat stat : stats) {
    /*
     * Dimension are CW's version of metric tags. A conversion must be done.
     */
    Collection<Dimension> metricDims = tagsToDimensions(stat.getTags());
    metricDims.addAll(parentDims);

    MetricDatum metric = new MetricDatum();
    metric.setMetricName(stat.getName());
    // TODO: add units to Stat object SYSTEMS-870
    metric.setUnit(StandardUnit.None);
    metric.setTimestamp(dt);
    metric.setDimensions(metricDims);
    metric.setValue((double) stat.getValue());

    metrics.add(metric);
  }

  /*
   * Not very well documented in java docs but CW only allows 20 metrics at a time.
   */
  List<List<MetricDatum>> chunks = ListUtils.partition(metrics, 20);
  for (List<MetricDatum> chunk : chunks) {
    PutMetricDataRequest req = new PutMetricDataRequest();
    req.withMetricData(chunk);
    req.setNamespace(namespace);

    this.client.putMetricData(req);
  }
}
 
Example 9
Source File: MkmOnlineExport.java    From MtgDesktopCompanion with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void exportDeck(MagicDeck deck, File dest) throws IOException {
	if(!init)
		init();

	
	WantsService wlService = new WantsService();
	List<WantItem> wants = new ArrayList<>();

	for (MagicCard mc : deck.getMain().keySet()) 
	{
		Integer p = null;
		try 
		{
			if(mc.getMkmId()!=null)
			{
				p = mc.getMkmId();
			}
			else
			{
				List<Product> list = pService.findProduct(mc.getName(), atts);
				if(!list.isEmpty())
				{
					logger.debug("found multiple product for " + mc +" : " + list.size());
					Product prod = MagicCardMarketPricer2.getProductFromCard(mc,list);
					
					if(prod!=null)
						p=prod.getIdProduct();
				}
			}
		}
		catch(Exception ex)
		{
			logger.error("could not export " + mc,ex);
			p=null;
		}	
	
		if (p != null) {
			WantItem w = new WantItem();
			w.setIdProduct(p);
			w.setCount(deck.getMain().get(mc));
			w.setFoil(new MkmBoolean(false));
			w.setMinCondition(getString(QUALITY));
			w.setAltered(new MkmBoolean(false));
			w.setType("product");
			w.setSigned(new MkmBoolean(false));
			for (String s : getArray(LANGUAGES))
				w.getIdLanguage().add(Integer.parseInt(s));
		
			wants.add(w);
		} else {
			logger.debug("could not find product for " + mc + " (" + mc.getCurrentSet()+")");
		}
		notify(mc);
	}

	int max = getInt(MAX_WANTLIST_SIZE);
	if (wants.size() <= max) {
		Wantslist l = wlService.createWantList(deck.getName());
		logger.debug("Create " + l + " list with " + wants.size() + " items");
		wlService.addItem(l, wants);
	} else {

		List<List<WantItem>> decoupes = ListUtils.partition(wants, max);

		for (int i = 0; i < decoupes.size(); i++) {
			Wantslist wl = wlService.createWantList(deck.getName() + "-" + (i + 1));
			logger.debug("Create " + wl + " list with " + decoupes.get(i).size() + " items");
			wlService.addItem(wl, decoupes.get(i));
		}

	}

}
 
Example 10
Source File: MyCollectionUtils.java    From spring-boot with Apache License 2.0 3 votes vote down vote up
/**
 * 单个 List ,分割成几个 subList , 保持原来 List 中元素的排序
 * 每个 subList 的大小不同,分割成的份数不同
 *
 * @param list the list to return consecutive sublists of
 * @param size 每个 sub list 的大小 the desired size of each sublist (the last may be smaller)
 *             大小不同,最后分割成的份数不同
 * @param <T>
 * @return
 */
@Deprecated  // 使用下面的  partition ,分组更合理
private static <T> List<List<T>> toSubList(List<T> list, int size) {
    //Guava
    //return Lists.partition(list, size);
    //Apache Commons Collections
    return ListUtils.partition(list, size);
}