欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java?list和map切割分段的實現(xiàn)及多線程應用案例

 更新時間:2023年12月24日 15:08:04   作者:蠶1蠶2  
這篇文章主要為大家介紹了java?list和map切割分段的實現(xiàn)及多線程應用案例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

map工具類

/**
 * @author chengxianwei
 * @title map工具類
 * @date 2023/08/03
 **/
public class MapUtil {
    /**
     * List拆分 (指定分組大小)
     *
     * @param chunkList 被分隔的數(shù)組
     * @param chunkNum  每段的大小
     * @param <T>       List中的類型
     * @return
     */
    public static <T> List<List<T>> listChunk(List<T> chunkList, int chunkNum) {
        if (chunkList == null || chunkNum <= 0) {
            List<List<T>> t = new ArrayList<>();
            t.add(chunkList);
            return t;
        }
        Iterator<T> iterator = chunkList.iterator();
        int i = 1;
        List<List<T>> total = new ArrayList<>();
        List<T> tem = new ArrayList<>();
        while (iterator.hasNext()) {
            T next = iterator.next();
            tem.add(next);
            if (i == chunkNum) {
                total.add(tem);
                tem = new ArrayList<>();
                i = 0;
            }
            i++;
        }
        if (!ChargeEmptyUtil.isEmpty(tem)) {
            total.add(tem);
        }
        return total;
    }
    /**
     * Map拆分 (指定分組大小)
     *
     * @param chunkMap 被切段的map
     * @param chunkNum 每段的大小
     * @param <k>      map的key類型
     * @param <v>      map的value類型 如果是自定義類型,則必須實現(xiàn)equals和hashCode方法
     * @return
     */
    public static <k, v> List<Map<k, v>> mapChunk(Map<k, v> chunkMap, int chunkNum) {
        if (chunkMap == null || chunkNum <= 0) {
            List<Map<k, v>> list = new ArrayList<>();
            list.add(chunkMap);
            return list;
        }
        Set<k> keySet = chunkMap.keySet();
        Iterator<k> iterator = keySet.iterator();
        int i = 1;
        List<Map<k, v>> total = new ArrayList<>();
        Map<k, v> tem = new HashMap<>();
        while (iterator.hasNext()) {
            k next = iterator.next();
            tem.put(next, chunkMap.get(next));
            if (i == chunkNum) {
                total.add(tem);
                tem = new HashMap<>();
                i = 0;
            }
            i++;
        }
        if (!ChargeEmptyUtil.isEmpty(tem)) {
            total.add(tem);
        }
        return total;
    }
    /**
     * Map拆分 (指定分組大小)
     *
     * @param map       Map
     * @param chunkSize 每個分組的大小 (>=1)
     * @param <K>       Key
     * @param <V>       Value
     * @return 子Map列表
     */
    public static <K, V> List<Map<K, V>> splitByChunkSize(Map<K, V> map, int chunkSize) {
        if (Objects.isNull(map) || map.isEmpty() || chunkSize < 1) {
            //空map或者分組大小<1,無法拆分
            return Collections.emptyList();
        }
        int mapSize = map.size(); //鍵值對總數(shù)
        int groupSize = mapSize / chunkSize + (mapSize % chunkSize == 0 ? 0 : 1); //計算分組個數(shù)
        List<Map<K, V>> list = Lists.newArrayListWithCapacity(groupSize); //子Map列表
        if (chunkSize >= mapSize) { //只能分1組的情況
            list.add(map);
            return list;
        }
        int count = 0; //每個分組的組內(nèi)計數(shù)
        Map<K, V> subMap = Maps.newHashMapWithExpectedSize(chunkSize); //子Map
        for (Map.Entry<K, V> entry : map.entrySet()) {
            if (count < chunkSize) {
                //給每個分組放chunkSize個鍵值對,最后一個分組可能會裝不滿
                subMap.put(entry.getKey(), entry.getValue());
                count++; //組內(nèi)計數(shù)+1
            } else {
                //結(jié)束上一個分組
                list.add(subMap); //當前分組裝滿了->加入列表
                //開始下一個分組
                subMap = Maps.newHashMapWithExpectedSize(chunkSize); //新的分組
                subMap.put(entry.getKey(), entry.getValue()); //添加當前鍵值對
                count = 1; //組內(nèi)計數(shù)重置為1
            }
        }
        list.add(subMap);  //添加最后一個分組
        return list;
    }
    /**
     * Map拆分(指定分組個數(shù))
     *
     * @param map       Map
     * @param groupSize 分組個數(shù) (>=1)
     * @param <K>       Key
     * @param <V>       Value
     * @return 子Map列表
     */
    public static <K, V> List<Map<K, V>> splitByGroupSize(Map<K, V> map, int groupSize) {
        if (Objects.isNull(map) || map.isEmpty() || groupSize < 1) {
            //空map或者分組數(shù)<1,無法拆分
            return Collections.emptyList();
        }
        List<Map<K, V>> list = Lists.newArrayListWithCapacity(groupSize);
        if (groupSize == 1) { //只有1個分組的情況
            list.add(map);
            return list;
        }
        int mapSize = map.size(); //鍵值對總數(shù)
        int chunkIndex = 0; //當前分組的下標,[0, groupSize-1]
        int restCount = mapSize % groupSize; //平均后剩余的鍵值對數(shù)
        int chunkSize0 = mapSize / groupSize; //每個分組鍵值對數(shù)量
        int chunkSize1 = chunkSize0 + 1; //多分一個
        int chunkSize = chunkIndex < restCount ? chunkSize1 : chunkSize0; //實際每組的大?。ㄇ懊娴牟糠址纸M可能會多分1個)
        int count = 0; //每個分組的組內(nèi)計數(shù)
        Map<K, V> subMap = Maps.newHashMapWithExpectedSize(chunkSize);//子Map
        for (Map.Entry<K, V> entry : map.entrySet()) {
            if (count < chunkSize) {
                //每個分組按實際分組大?。╟hunkSize)加入鍵值對
                subMap.put(entry.getKey(), entry.getValue());
                count++; //組內(nèi)計數(shù)+1
            } else {
                //結(jié)束上一個分組
                list.add(subMap); //當前分組裝滿了->加入列表
                chunkIndex++; //分組個數(shù)+1
                //開始下一個分組
                chunkSize = chunkIndex < restCount ? chunkSize1 : chunkSize0; //重新計算分組大小
                subMap = Maps.newHashMapWithExpectedSize(chunkSize); //新的分組
                subMap.put(entry.getKey(), entry.getValue()); //添加當前鍵值對
                count = 1; //組內(nèi)計數(shù)重置為1
            }
        }
        list.add(subMap); //添加最后一個分組
        return list;
    }
    //測試
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("a", "1");
        map.put("b", "2");
        map.put("c", "3");
        map.put("d", "4");
        map.put("e", "5");
        map.put("f", "6");
        map.put("g", "7");
        System.out.println("splitByChunkSize: " + splitByChunkSize(map, 3));
        System.out.println("splitByGroupSize: " + splitByGroupSize(map, 3));
    }
}

應用案例

List分段

{
            // 導出動態(tài)標題
            List<String> titleList = new ArrayList<>();
            // 上網(wǎng)電量和發(fā)電量
            String[] monthKeyArr = {"powernetday", "powerday"};
            // 獲取導出電站基礎數(shù)據(jù)
            List<XczxPoverPhotovolSourceVO>[0] = new ArrayList<>();
            // 保存成<consNo, <日電量數(shù)據(jù)列表>> 形式
            ConcurrentHashMap<String, List<XczxPoverPhotovolSourceDayEleVO>> xczxPovertyPowerDayListMap = new ConcurrentHashMap<>();
            ...
            ...
            ...
            // 多線程分段批量處理List列表
            int threadSize = 2000;//每2000條開啟一個任務
            int dataSize = xczxPoverPhotovolSourceVOList[0].size();
            int threadNum = dataSize / threadSize + 1;// 開啟的任務數(shù)
            boolean special = dataSize % threadSize ==0;// 表計整除 (threadNum多算了一次)
            Callable<Integer> task = null;
            List<XczxPoverPhotovolSourceVO> curList = null;
            List<Callable<Integer>> tasks = new ArrayList<>();
            ExecutorService exec = Executors.newCachedThreadPool();
            CountDownLatch countDownLatch = new CountDownLatch(threadNum);
            // 使用Collections將線程不安全的集合轉(zhuǎn)成線程安全的集合
            List<Map<String, Object>> dataList = Collections.synchronizedList(new ArrayList<>(dataSize));
            for (int i = 0; i < threadNum; i++) {
                if (i == threadNum - 1) {
                    if(special){
                        countDownLatch.countDown();
                        break;
                    }
                    curList = xczxPoverPhotovolSourceVOList[0].subList(threadSize * i, dataSize);
                } else {
                    curList = xczxPoverPhotovolSourceVOList[0].subList(threadSize * i, threadSize * (i + 1));
                }
                final List<XczxPoverPhotovolSourceVO> list2 = curList;
                task = new Callable<Integer>() {
                    @Override
                    public synchronized Integer call() throws Exception {
                        try {
                            XczxPoverPhotovolSourceVO next = null;
                            Date parallelInTime = null;
                            Map<String, Object> map = null;
                            List<Map<String, Object>> totalList = null;
                            Map<String, Object> totalMap = null;
                            List<Map<String, Object>> povertyList = null;
                            Map<String, Object> entityMap = null;
                            XczxPoverPhotovolSourceDayEleVO next2 = null;
                            XczxPoverPhotovolSourceDayEleVO curDayEle = null;
                            List<XczxPoverPhotovolSourceDayEleVO> list = null;
                            List<XczxPoverPhotovolSourceDayEleVO> listDay = new ArrayList<>();
                            // 迭代導出電站基礎數(shù)據(jù)
                            Iterator<XczxPoverPhotovolSourceVO> iterator = list2.iterator();
                            while (iterator.hasNext()) {
                                next = iterator.next();
                                map = CommonUtils.transClassToMapLowCase(next);
                                // 合計 集合
                                totalList = new ArrayList<>();
                                totalMap = new HashMap<>();
                                // 合計
                                BigDecimal powerNetTotal = new BigDecimal(BigInteger.ZERO);
                                BigDecimal powerTotal = new BigDecimal(BigInteger.ZERO);
                                // 指定日期的 戶號列表 17123
                                listDay = xczxPovertyPowerDayListMap.get(""+next.getId());
                                if (listDay != null && listDay.size() > 0) {
                                    // 遍歷一個戶號 所有日期
                                    for (int i = 0; i < titleList.size() - 1; i++) {
                                        String date = titleList.get(i).substring(0, 4) + titleList.get(i).substring(5, 7) + titleList.get(i).substring(8, 10);
                                        list = listDay.stream().filter(x -> date.equals(x.getPovertyDay())).collect(Collectors.toList());
                                        povertyList = new ArrayList<>();
                                        entityMap = new HashMap<>();
                                        if (list != null && list.size() > 0) {
                                            for (XczxPoverPhotovolSourceDayEleVO vo : list) {
                                                entityMap.put(monthKeyArr[0] + titleList.get(i), new BigDecimal(vo.getPowerNetDay()));
                                                entityMap.put(monthKeyArr[1] + titleList.get(i), new BigDecimal(vo.getPowerDay()));
                                                powerNetTotal = powerNetTotal.add(new BigDecimal(vo.getPowerNetDay()));
                                                powerTotal = powerTotal.add(new BigDecimal(vo.getPowerDay()));
                                                povertyList.add(entityMap);
                                            }
                                        } else {
                                            // 如果為空,填充0
                                            entityMap.put(monthKeyArr[0] + titleList.get(i), "0");
                                            entityMap.put(monthKeyArr[1] + titleList.get(i), "0");
                                            povertyList.add(entityMap);
                                        }
                                        map.put(day + titleList.get(i), povertyList);
                                    }
                                } else {
                                    // 遍歷一個戶號 所有日期
                                    for (int i = 0; i < titleList.size() - 1; i++) {
                                        povertyList = new ArrayList<>();
                                        entityMap = new HashMap<>();
                                        // 如果為空,填充0
                                        entityMap.put(monthKeyArr[0] + titleList.get(i), "0");
                                        entityMap.put(monthKeyArr[1] + titleList.get(i), "0");
                                        povertyList.add(entityMap);
                                        povertyList.add(entityMap);
                                        map.put(day + titleList.get(i), povertyList);
                                    }
                                }
                                // 合計模型
                                totalMap.put(monthKeyArr[0] + "合計", powerNetTotal);
                                totalMap.put(monthKeyArr[1] + "合計", powerTotal);
                                totalList.add(totalMap);
                                map.put(day + "合計", totalList);
                                dataList.add(map);
                                map.put("xh", dataList.size());
                            }
                        } catch (Exception e) {
                            log.error("catch error",e);
                        } finally {
                            countDownLatch.countDown();
                            return 1;
                        }
                    }
                };
                tasks.add(task);
            }
            // 多線程并發(fā)
            List<Future<Integer>> futureList = null;
            try {
                futureList = exec.invokeAll(tasks);
                for(Future<Integer> future : futureList){
                    if(future!=null) {
                        while (!future.isDone());
                    }
                }
                countDownLatch.await();
            } catch (Exception e) {
                log.error("catch error",e);
            } finally {
                exec.shutdown();
            }
            ...
            ...
            ...
}

Map分段

{
           ...
           ...
           ...
           // <戶主身份證號, List<戶主成員(含戶主)>> 集合檔案信息
           Map<String, List<XczxArchives>> impListMap = new HashMap<>();
           ...
           ...
           ...
           // 多線程分段批量處理Map集合
           int threadSize = 2000;// 每2000條開啟一個任務
           int dataSize = impListMap.size();// map數(shù)據(jù)條數(shù)
           int threadNum = dataSize / threadSize + 1;// 開啟的任務數(shù)
           List<Callable<Integer>> tasks = new ArrayList<>();
           ExecutorService exec = Executors.newCachedThreadPool();
           List<Map<String, List<XczxArchives>>> splitGroup = MapUtil.splitByChunkSize(impListMap, threadSize);
           for (int i = 0; i < threadNum; i++) {
               Map<String, List<XczxArchives>> curLists = splitGroup.get(i);
               // 新建一個并發(fā)任務
               Callable<Integer> task = new Callable<Integer>() {
                   @Override
                   public Integer call() throws Exception {
                       int count = 0;
                       // iterator遍歷分段Map
                       Iterator<Map.Entry<String, List<XczxArchives>>> entryIterator = curLists.entrySet().iterator();
                       while (entryIterator.hasNext()) {
                           XczxArchives head = null;
                           Map.Entry<String, List<XczxArchives>> entry = entryIterator.next();
                           String key = entry.getKey();// 戶主身份證號
                           List<XczxArchives> list = entry.getValue();// 戶主及成員列表
                           Iterator<XczxArchives> dataIterator = list.iterator();
                           while (dataIterator.hasNext()) {
                               XczxArchives next = dataIterator.next();
                               if (key.equals(next.getIdCard())) {
                                   head = next;
                                   dataIterator.remove();
                               }
                           }
                           if (head != null) {
                               // 插入戶主
                               head.setHeadName(null);
                               head.setHeadIdCard(null);
                               head.setHeadMobile(null);
                               head.setCreateTime(date);
                               head.setUpdateTime(date);
                               int result = xczxArchivesMapper.insert(head);
                               if (result == 1) {
                                   count++;
                                   if (list != null && list.size() > 0) {
                                       for (XczxArchives next : list) {
                                           next.setCreateTime(date);
                                           next.setUpdateTime(date);
                                           next.setHeadId(head.getArchivesId());
                                       }
                                       // 插入成員
                                       int result2 = xczxArchivesMapper.batchInsert(list);
                                       if (result2 > 0) {
                                           count = count + result2;
                                       }
                                   }
                               }
                           }
                       }
                       return count;
                   }
               };
               tasks.add(task);
           }
           // 多線程并發(fā)
           List<Future<Integer>> futureList = null;
           try {
               futureList = exec.invokeAll(tasks);
               for (Future<Integer> future : futureList) {
                   if (future != null) {
                       while (!future.isDone());
                       successCount = successCount + future.get();
                   }
               }
           } catch (Exception e) {
               log.error("catch error",e);
           } finally {
               exec.shutdown();
           }
           ...
           ...
           ...
}

以上就是java list和map切割分段的實現(xiàn)及多線程應用案例的詳細內(nèi)容,更多關于java list map多線程切割分段的資料請關注腳本之家其它相關文章!

相關文章

  • java中字符串參數(shù)化符號${}的解析

    java中字符串參數(shù)化符號${}的解析

    這篇文章主要給大家介紹了java中字符串參數(shù)化符號${}是如何解析的,文中給出了詳細的分析與解決代碼,有需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-01-01
  • Java阻塞隊列中的BlockingQueue接口詳解

    Java阻塞隊列中的BlockingQueue接口詳解

    這篇文章主要介紹了Java阻塞隊列中的BlockingQueue接口詳解,對于Queue而言,BlockingQueue是主要的線程安全的版本,具有阻塞功能,可以允許添加、刪除元素被阻塞,直到成功為止,BlockingQueue相對于Queue而言增加了兩個方法put、take元素,需要的朋友可以參考下
    2023-09-09
  • java面試常見模式問題---單例模式

    java面試常見模式問題---單例模式

    單例模式(Singleton Pattern)是 Java 中最簡單的設計模式之一。這種類型的設計模式屬于創(chuàng)建型模式,它提供了一種創(chuàng)建對象的最佳方式
    2021-06-06
  • Java IO文件編碼轉(zhuǎn)換實現(xiàn)代碼

    Java IO文件編碼轉(zhuǎn)換實現(xiàn)代碼

    這篇文章主要介紹了Java IO文件編碼轉(zhuǎn)換實現(xiàn)代碼,有需要的朋友可以參考一下
    2013-12-12
  • 深入理解 Java注解及實例

    深入理解 Java注解及實例

    這篇文章主要介紹了深入理解 Java注解及實例的相關資料,希望通過本文大家能夠掌握java注解的知識,需要的朋友可以參考下
    2017-09-09
  • SpringBoot 整合 RabbitMQ 的使用方式(代碼示例)

    SpringBoot 整合 RabbitMQ 的使用方式(代碼示例)

    本文詳細介紹了使用RabbitTemplate進行消息傳遞的幾種模式,包括點對點通信、發(fā)布/訂閱模式、工作隊列模式、路由模式和主題模式,每種模式都通過代碼示例展示了生產(chǎn)者和消費者的實現(xiàn),幫助開發(fā)者理解和運用RabbitMQ進行高效的消息處理
    2024-10-10
  • SpringBoot2整合Redis實現(xiàn)讀寫操作

    SpringBoot2整合Redis實現(xiàn)讀寫操作

    Redis,對于大家來說應該不陌生,是經(jīng)常使用的開發(fā)技術之一。本文將結(jié)合實例代碼,介紹SpringBoot2整合Redis實現(xiàn)讀寫操作,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Java的MyBatis框架中MyBatis Generator代碼生成器的用法

    Java的MyBatis框架中MyBatis Generator代碼生成器的用法

    這篇文章主要介紹了Java的MyBatis框架中Mybatis Generator代碼生成器的用法,Mybatis Generator主要被用來生成繁瑣的配置文件來提高效率,需要的朋友可以參考下
    2016-04-04
  • 高內(nèi)聚低耦合法則實例解析

    高內(nèi)聚低耦合法則實例解析

    這篇文章主要介紹了高內(nèi)聚低耦合法則實例代碼解析,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • 詳解SpringBoot中Session超時原理說明

    詳解SpringBoot中Session超時原理說明

    本篇文章主要介紹了詳解SpringBoot中Session超時原理說明,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08

最新評論