当前位置:   article > 正文

java中Map List的用法_定义map

定义map

返回的restful接口数据样式:

{
     party: [
         {
             "finishTime": "20191105",
             "busiCnt": 1000
         }
     ]
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

1、定义WzwScreenEntity 实体

public class WzwScreenEntity {

    private String finishTime;
    private String busiCnt;
    private String party;

    public String getFinishTime() {
        return finishTime;
    }

    public void setFinishTime(String finishTime) {
        this.finishTime = finishTime;
    }

    public String getBusiCnt() {
        return busiCnt;
    }

    public void setBusiCnt(String busiCnt) {
        this.busiCnt = busiCnt;
    }

    public String getParty() {
        return party;
    }

    public void setParty(String party) {
        this.party = party;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

2、定义list实体

public class WzwScreenChildEntity {
    public String getFinishTime() {
        return finishTime;
    }

    public void setFinishTime(String finishTime) {
        this.finishTime = finishTime;
    }

    public String getBusiCnt() {
        return busiCnt;
    }

    public void setBusiCnt(String busiCnt) {
        this.busiCnt = busiCnt;
    }

    private String finishTime;
    private String busiCnt;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

3、具体实现

List<WzwScreenEntity> scList = new ArrayList<>();
scList  = mapper.queryData();//从数据库获取数据
Map<String, List> map = new HashMap<>();//定义返回的map

if (scList.size() != 0) {
    for (WzwScreenEntity entity : scList) {
        WzwScreenChildEntity childEntity = new WzwScreenChildEntity();
        childEntity.setFinishTime(entity.getFinishTime());
        childEntity.setBusiCnt(entity.getBusiCnt());

        List<WzwScreenChildEntity> list = new ArrayList<>();
        list.add(childEntity);

		//当map中的key相同的,防止value值被覆盖。
        if (map.containsKey(entity.getParty())) {
            list.addAll(map.get(entity.getParty()));
            map.put(entity.getParty(), list);
        } else {
            map.put(entity.getParty(), list);
        }
    }
}
LOGGER.info("map================== " + map);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/261828
推荐阅读
相关标签
  

闽ICP备14008679号