闲释 2023-03-29 10:29 采纳率: 100%
浏览 76
已结题

Java集合多对一问题集

要求将B实体集合list循环最少次数转存成A集合实体list

A集合实体类 , 其中数据更新时间和站点code为主键


@Data
@TableName("moni_local_site_hour")
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
@ApiModel(value="moni_local_site_hour对象", description="本地站点小时数据")
public class MoniLocalSiteHour implements Serializable {
    private static final long serialVersionUID = 1L;

    /**数据更新时间*/
    @Excel(name = "数据更新时间", width = 15, format = "yyyy-MM-dd")
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "数据更新时间")
    private Date datatime;
    /**站点Code*/
    @Excel(name = "站点Code", width = 15)
    @ApiModelProperty(value = "站点Code")
    private String stationcode;
    /**站点名称*/
    @Excel(name = "站点名称", width = 15)
    @ApiModelProperty(value = "站点名称")
    private String stationname;
    /**二氧化硫浓度值*/
    @Excel(name = "二氧化硫浓度值", width = 15)
    @ApiModelProperty(value = "二氧化硫浓度值")
    private Integer so2;
    /**二氧化氮浓度值*/
    @Excel(name = "二氧化氮浓度值", width = 15)
    @ApiModelProperty(value = "二氧化氮浓度值")
    private Integer no2;
    /**细颗粒物浓度值*/
    @Excel(name = "细颗粒物浓度值", width = 15)
    @ApiModelProperty(value = "细颗粒物浓度值")
    private Integer pm25;
    /**可吸入颗粒物浓度值*/
    @Excel(name = "可吸入颗粒物浓度值", width = 15)
    @ApiModelProperty(value = "可吸入颗粒物浓度值")
    private Integer pm10;
    /**一氧化碳浓度值*/
    @Excel(name = "一氧化碳浓度值", width = 15)
    @ApiModelProperty(value = "一氧化碳浓度值")
    private BigDecimal co;
    /**臭氧浓度值*/
    @Excel(name = "臭氧浓度值", width = 15)
    @ApiModelProperty(value = "臭氧浓度值")
    private Integer o3;
    /**PM2.5分指数*/
    @Excel(name = "PM2.5分指数", width = 15)
    @ApiModelProperty(value = "PM2.5分指数")
    private Integer pm25iaqi;
    /**二氧化硫分指数*/
    @Excel(name = "二氧化硫分指数", width = 15)
    @ApiModelProperty(value = "二氧化硫分指数")
    private Integer so2iaqi;
    /**二氧化氮分指数*/
    @Excel(name = "二氧化氮分指数", width = 15)
    @ApiModelProperty(value = "二氧化氮分指数")
    private Integer no2iaqi;
    /**PM10分指数*/
    @Excel(name = "PM10分指数", width = 15)
    @ApiModelProperty(value = "PM10分指数")
    private Integer pm10iaqi;
    /**一氧化碳分指数*/
    @Excel(name = "一氧化碳分指数", width = 15)
    @ApiModelProperty(value = "一氧化碳分指数")
    private Integer coiaqi;
    /**臭氧分指数*/
    @Excel(name = "臭氧分指数", width = 15)
    @ApiModelProperty(value = "臭氧分指数")
    private Integer o3iaqi;
    /**空气质量指数*/
    @Excel(name = "空气质量指数", width = 15)
    @ApiModelProperty(value = "空气质量指数")
    private Integer aqi;
    /**首要污染物*/
    @Excel(name = "首要污染物", width = 15)
    @ApiModelProperty(value = "首要污染物")
    private String primarypol;
    /**AQI等级*/
    @Excel(name = "AQI等级", width = 15)
    @ApiModelProperty(value = "AQI等级")
    private String aqilevel;
    /**臭氧8小时浓度值*/
    @Excel(name = "臭氧8小时浓度值", width = 15)
    @ApiModelProperty(value = "臭氧8小时浓度值")
    @TableField("o3_8h")
    private Integer o38h;
    /**臭氧8小时分指数*/
    @Excel(name = "臭氧8小时分指数", width = 15)
    @ApiModelProperty(value = "臭氧8小时分指数")
    @TableField("o3_8hiaqi")
    private Integer o38hiaqi;
    /**综合指数*/
    @Excel(name = "综合指数", width = 15)
    @ApiModelProperty(value = "综合指数")
    private BigDecimal countindex;
    /**flag*/
    @Excel(name = "flag", width = 15)
    @ApiModelProperty(value = "flag")
    private Integer flag;
    /**
     * 数据类型0:原始实况,1:审核实况,2:原始标况,3:审核标况
     */
    private Integer dataType;
    /**创建时间*/
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "创建时间")
    private Date createTime;
    /**修改时间*/
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "修改时间")
    private Date updateTime;

    /**温度*/
    @Excel(name = "温度", width = 15)
    @ApiModelProperty(value = "温度")
    private BigDecimal temp;
    /**相对湿度*/
    @Excel(name = "相对湿度", width = 15)
    @ApiModelProperty(value = "相对湿度")
    private BigDecimal rh;
    /**气压*/
    @Excel(name = "气压", width = 15)
    @ApiModelProperty(value = "气压")
    private BigDecimal pres;
    /**风速*/
    @Excel(name = "风速", width = 15)
    @ApiModelProperty(value = "风速")
    private BigDecimal speed;
    /**风向*/
    @Excel(name = "风向", width = 15)
    @ApiModelProperty(value = "风向")
    private BigDecimal wind;

}

B集合实体


```java

@Data
public class StationHourOrDayData {

    /**
     * 时间
     */
    @Excel(name = "时间", width = 15, format = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "时间")
    private Date collectDatetime;
    /**
     * 是否异常(0:异常 1:正常)
     */
    private Integer isNormal;
    /**
     * 污染等级
     */
    private String level;
    /**
     * 污染物浓度值
     */
    private Integer pollutantValue;
    /**
     * 污染物ID
     */
    @TableField(exist = false)
    private String pollutantId;
    /**
     * 站点code
     */
    @TableField(exist = false)
    private String stationId;
    /**
     * 站点名称
     */
    @TableField(exist = false)
    private String address;
    /**
     * 具体污染物(so2 / no2 / pm25 / pm10 / co / o3 / aqi  )
     */
    @TableField(exist = false)
    private String pollutantType;


}

```

  • 写回答

4条回答 默认 最新

  • 社区专家-Monster-XH 2023-03-29 10:42
    关注

    基于Monster 组和GPT的调写:大概思路:通过Map将B集合按照主键(datatime + stationcode)分组,然后遍历Map,创建A集合对象并将相应的值复制到A对象中。

    
    // 假设B集合为List<MoniLocalSiteHour>类型,A集合为List<MoniLocalSiteHour>
    List<MoniLocalSiteHour> bList = ...
    
    // 创建Map,按照主键(datatime + stationcode)分组
    Map<String, MoniLocalSiteHour> map = new HashMap<>();
    for (MoniLocalSiteHour b : bList) {
        String key = b.getDatatime().getTime() + "_" + b.getStationcode();
        MoniLocalSiteHour a = map.get(key);
        if (a == null) {
            a = new MoniLocalSiteHour();
            a.setDatatime(b.getDatatime());
            a.setStationcode(b.getStationcode());
            map.put(key, a);
        }
        // 复制属性值到A对象中
        a.setStationname(b.getStationname());
        a.setSo2(b.getSo2());
        a.setNo2(b.getNo2());
        a.setPm25(b.getPm25());
        a.setPm10(b.getPm10());
        a.setCo(b.getCo());
        a.setO3(b.getO3());
        a.setPm25iaqi(b.getPm25iaqi());
        a.setSo2iaqi(b.getSo2iaqi());
        a.setNo2iaqi(b.getNo2iaqi());
        a.setPm10iaqi(b.getPm10iaqi());
        a.setCoiaqi(b.getCoiaqi());
        a.setO3iaqi(b.getO3iaqi());
        a.setAqi(b.getAqi());
        a.setPrimarypol(b.getPrimarypol());
        a.setAqilevel(b.getAqilevel());
        a.setO38h(b.getO38h());
    }
    
    // 将Map中的值存入A集合中
    List<MoniLocalSiteHour> aList = new ArrayList<>(map.values());
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 4月11日
  • 已采纳回答 4月3日
  • 创建了问题 3月29日