将List中的实体类按照实体类的某个字段分组

数据库中查询到的数据为List<Report>,将数据处理再返回

public Map<String, List<Report>> findAllReport(String projectId) throws FindAllReportException {
//根据ID找到项目编号
Project project=projectDao.findProjectById(projectId);
String projectNO=project.getNO();

List<Report> list=dao.findReports(projectNO);

Map<String,List<Report>> resultMap=new HashMap<String,List<Report>>();

//遍历List<Report>
for(Report i:list) {
  if(resultMap.containsKey(i.getName())) {//map中name已存在,存放到同一个key下的value中
    resultMap.get(i.getName()).add(i);
  }else {//map中不存在,新建key,用来存放数据
    List<Report> newList=new ArrayList<Report>();
    newList.add(i);
    resultMap.put(i.getName(), newList);
  }
}

return resultMap;
}

//前台用$.each()来遍历map

$.each(map,function(key,value){

  console.log(key);

  //遍历value

  for(var i=0;i<value.length;i++){

    console.log(value[i]);

  }   

}

)
posted @ 2018-03-16 10:49  路边一草鞋  阅读(609)  评论(0编辑  收藏  举报