zoukankan      html  css  js  c++  java
  • 【java】将List中的实体按照某个字段进行分组的算法

    如何将List中存放的实体按照某个字段进行分组呢?来看看下面的例子,假如实体中有个字段叫批次号,我们将具有相同批次号的实体放在一起,那么怎么实现呢?看下面的代码:

    可以定义个Map,Map的key用于存放异常批次号,value存放实体集合List<TmExcpNewVo>。循环要排序的List

    /**
    * 对list进行分组
    * @param billingList
    * @return
    * @throws Exception
    */
    private Map<String, List<LogSys>> groupBillingDataByExcpBatchCode(List<LogSys> billingList) throws Exception{
    Map<String, List<LogSys>> resultMap = new HashMap<String, List<LogSys>>();

    try{
    for(LogSys tmExcpNew : billingList){

    if(resultMap.containsKey(tmExcpNew.getRecordCount())){//map中异常批次已存在,将该数据存放到同一个key(key存放的是异常批次)的map中
    resultMap.get(tmExcpNew.getRecordCount()).add(tmExcpNew);
    }else{//map中不存在,新建key,用来存放数据
    List<LogSys> tmpList = new ArrayList<LogSys>();
    tmpList.add(tmExcpNew);
    resultMap.put(tmExcpNew.getRecordCount(), tmpList);

    }

    }

    }catch(Exception e){
    e.printStackTrace();
    }

    return resultMap;
    }

  • 相关阅读:
    js 和 jquery的宽高
    client、offset、scroll
    web开发中会话跟踪的方法有哪些
    前端需要注意哪些SEO
    ES6 Set和Map数据结构
    ES6实现数组去重
    ES6 Symbol
    ES6对象的拓展
    ES6数组的拓展
    ES6函数的拓展
  • 原文地址:https://www.cnblogs.com/shizhijie/p/7698094.html
Copyright © 2011-2022 走看看