本人在做项目中,遇到需求是要求在页面展示一到十二月份(month_)的相对应的数据(num_),month_与num_都是从数据库中获取的到的,一到十二月可能获取到是只有一两个月,其他没有的月份则需要补全且num_值默认为0
从数据库获取的示例如下:
[{sum_=22, month_=10},{sum_=15, month_=09}]
我的思路是通过建立数组存放固定的十二个月份的值,然后通过两层for循环把值加入需要返回的前端,但在这个过程会出现重复的值,为了去重,我采用了Set集合来存放,大部分数据被去重后,但仍然会出现下面的:
[{sum_=0, month_=01}, {sum_=0, month_=12}, {sum_=0, month_=11}, {sum_=0, month_=03}, {sum_=22, month_=10}, {sum_=0, month_=02}, {sum_=0, month_=05},{sum_=0, month_=10}, {sum_=0, month_=04}, {sum_=0, month_=07}, {sum_=0, month_=06}, {sum_=0, month_=09}, {sum_=0, month_=08},{sum_=15, month_=09}]
[{sum_=22, month_=10}]循环添加后会再出现一个 [{sum_=0, month_=10}] 这样的数据无用的数据,该怎么办呢
于是我又通过遍历最后的值,反向循环后利用map的key键不可重复特性,先获取month_的值,再移除month_,然后再把month_作为key,把sum_作为value,存在单个map中,于是就得到了以下数据,如果
{11={sum_=0}, 12={sum_=0}, 01={sum_=0}, 02={sum_=0}, 03={sum_=0}, 04={sum_=0}, 05={sum_=0}, 06={sum_=0}, 07={sum_=0}, 08={sum_=0}, 09={sum_=0}, 10={sum_=22}}
这个时候又会发现九月份的数据被覆盖掉了,是不是有点头疼的,于是我想到在去重之前先给集合里数据排序再去重就好了,
最后才得到自己想要的数据,如下
{
"msg": "查询成功",
"data": [
{
"sum_": 0,
"month_": "11"
},
{
"sum_": 0,
"month_": "12"
},
{
"sum_": 0,
"month_": "01"
},
{
"sum_": 0,
"month_": "02"
},
{
"sum_": 0,
"month_": "03"
},
{
"sum_": 0,
"month_": "04"
},
{
"sum_": 0,
"month_": "05"
},
{
"sum_": 0,
"month_": "06"
},
{
"sum_": 0,
"month_": "07"
},
{
"sum_": 0,
"month_": "08"
},
{
"sum_":"15",
"month_": "09"
},
{
"sum_": "22",
"month_": "10"
}
],
"success": true
}
具体实现核心代码
public Map<String, Object> filingSituation(HttpServletRequest request) {
Map<String,Object> map = new HashMap<>();
Set<Map<String, Object>> data = new HashSet<>();
try {
String[] months= {"01","02","03","04","05","06","07","08","09","10","11","12"};
String year = request.getParameter("year");
if (null == year || "".equals(year)) {
Calendar now = Calendar.getInstance();
year = String.valueOf(now.get(Calendar.YEAR));
}
Map<String,Object> paramMap = new HashMap<>();
paramMap.put("year",year);
paramMap.put("superviseunit","20191026161819242594");//用于测试
// paramMap.put("superviseunit",getUserInfo().getSsdwid());
List<Map<String, Object>> list = personBasicMapper.fillingSituation(paramMap);
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < months.length; j++) {
String month_ = list.get(i).get("month_").toString();
String sum_ = list.get(i).get("sum_").toString();
if(!months[j].equals(month_)) {
Map<String,Object> resultMap = new HashMap();
resultMap.put("month_", months[j]);
resultMap.put("sum_", 0);
data.add(resultMap);
}else {
Map<String,Object> resultMap = new HashMap();
resultMap.put("month_",month_ );
resultMap.put("sum_",sum_);
data.add(resultMap);
}
}
}
List<Map<String, Object>> listMap = new ArrayList<>();
List<Map<String,Object>> dataList = new ArrayList<>(data);
Collections.sort(dataList, new Comparator<Map<String,Object>>(){
public int compare(Map<String,Object> o1,Map<String,Object> o2){
return o2.get("sum_").toString().compareTo(o1.get("sum_").toString());
}
});
Map<String,Map> mmp = new HashMap<>();
for(int k = dataList.size()-1; k >= 0; k--) {
Map<String, Object> mpp = dataList.get(k);
String month = mpp.get("month_").toString();
mpp.remove("month_");
mmp.put(month, mpp);
}
Set<String> keySet = mmp.keySet();
for (String st : keySet) {
Map newMap = mmp.get(st);
newMap.put("month_", st);
listMap.add(newMap);
}
ReturnUtil.createSuccess(map,listMap,CommonRest.QUERY_SUCCESS);
} catch (Exception e) {
e.printStackTrace();
ReturnUtil.createError(map, CommonRest.QUERY_FAILD);
}
return map;
}
以上仅供参考学习,有疑问请留言,觉得不错请点个赞,谢谢!