将当前行与上一行进行比较。 Java
Comparing the current line with the previous one. Java
我想将当前行与上一行进行比较,如果匹配,则进行计算。如果不是,则分配 0.0 我的代码只计算而不接受条件。
例如我有table:石油
oil 包含月份、日期和指标
它必须看起来像:
Month Values Date |formula |Growth
september 100 01.09.2020 |(100/nan - 1) * 100 |infinity
september 80 08.09.2020 |(80/100-1) * 100 |-20
september 100 15.09.2020 |(100/80-1) * 100 |25
october 118 01.10.2020 |september != october |0
october 120 08.10.2020 |(120/118-1) * 100 |1.69
october 123 15.10.2020 |(123/120-1) * 100 |2.5
november 122 01.11.2020 |october != november |0
november 122 08.11.2020 |(122/122-1) * 100 |0
我的class模特:
@Entity
@Table(name = "oper_mon_oil_price")
public class OilModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "o_year")
private String oyear;
@Column(name = "o_month")
private String omonth;
@Column(name = "o_date_event")
private String odateEvent;
@Column(name = "o_period")
private String operiod;
@Column(name = "o_area")
private String oarea;
@Column(name = "o_unit")
private String ounit;
@Column(name = "o_ai_92")
public Double oai92;
@Column(name = "o_ai_95")
public Double oai95;
@Column(name = "o_diesel_winter")
private Double odieselWinter;
@Column(name = "o_diesel_fuel_offseason")
private Double odieselFuelOffseason;
@Column(name = "growth_ai_92")
public Double growthAi92;
@Column(name = "growth_ai_95")
public Double growthAi95;
@Column(name = "growth_diesel_winter")
public Double growthDieselWinter;
@Column(name = "growth_diesel_fuel_offseason")
public Double growthDieselFuelOffseason;
getters/setters
}
class 存储库:
@Repository
@Mapper
public interface OilProdTovRepo {
@Select("SELECT * FROM oper_mon_prod_tov ORDER BY o_year ASC, o_month ASC, o_date_event ASC ")
@Results(value ={
@Result(property = "id",column = "id"),
@Result(property = "area",column = "o_area"),
@Result(property = "year",column = "o_year"),
@Result(property = "month",column = "o_month"),
@Result(property = "period",column = "o_period"),
@Result(property = "dateEvent",column = "o_date_event"),
@Result(property = "unit",column = "o_unit"),
@Result(property = "wheatFlourFirstGrade",column = "o_wheat_flour_first_grade"),
@Result(property = "wheatBread",column = "o_wheat_bread"),
@Result(property = "horns",column = "o_horns"),
@Result(property = "buckwheat",column = "o_buckwheat"),
@Result(property = "polishedRice",column = "o_polished_rice"),
@Result(property = "potato",column = "o_potato"),
@Result(property = "carrots",column = "o_carrots"),
@Result(property = "onion",column = "o_onion"),
@Result(property = "cabbageWhite",column = "o_cabbage_white"),
@Result(property = "sugar",column = "o_sugar"),
@Result(property = "oil",column = "o_oil"),
@Result(property = "beef",column = "o_beef"),
@Result(property = "chickenMeat",column = "o_chicken_meat"),
@Result(property = "milk",column = "o_milk"),
@Result(property = "kefir",column = "o_kefir"),
@Result(property = "butter",column = "o_butter"),
@Result(property = "egg",column = "o_egg"),
@Result(property = "salt",column = "o_salt"),
@Result(property = "cottageCheese",column = "o_cottage_cheese"),
@Result(property = "growthWheatFlourFirstGrade",column = "o_growth_wheat_flour_first_grade"),
})
List<OilProdTovModel> getOilProdTovModel();
@Update("UPDATE oper_mon_prod_tov SET o_growth_wheat_flour_first_grade=#{growthWheatFlourFirstGrade} WHERE id=#{id}")
void updateWheatFlourFirstGrade(OilProdTovModel oilProdTovModel);
}
和主要class,服务:
@Service
public class OilProdTovService {
@Autowired
public OilProdTovRepo oilProdTovRepo;
public List<OilProdTovModel> allOilProdTov() {
List<OilProdTovModel> oilProdTovModels = oilProdTovRepo.getOilProdTovModel();
Double first = 0.0;
Double result;
for (OilProdTovModel oilProdTovModel : oilProdTovModels) {
if (oilProdTovModel.getMonth().equals("September")) {
Double wheatFlourFirstGrade = oilProdTovModel.getWheatFlourFirstGrade();
result = (wheatFlourFirstGrade / first - 1) * 100;
first = wheatFlourFirstGrade;
wheatFlourFirstGrade = result;
oilProdTovModel.setGrowthWheatFlourFirstGrade(wheatFlourFirstGrade);
} else if (oilProdTovModel.getMonth().equals("October")) {
Double wheatFlourFirstGrade = oilProdTovModel.getWheatFlourFirstGrade();
result = (wheatFlourFirstGrade / first - 1) * 100;
first = wheatFlourFirstGrade;
wheatFlourFirstGrade = result;
oilProdTovModel.setGrowthWheatFlourFirstGrade(wheatFlourFirstGrade);
}
else if (oilProdTovModel.getMonth().equals("November")) {
Double wheatFlourFirstGrade = oilProdTovModel.getWheatFlourFirstGrade();
result = (wheatFlourFirstGrade / first - 1) * 100;
first = wheatFlourFirstGrade;
wheatFlourFirstGrade = result;
oilProdTovModel.setGrowthWheatFlourFirstGrade(wheatFlourFirstGrade);
}
else {
oilProdTovModel.setGrowthWheatFlourFirstGrade(0.0);
}
oilProdTovRepo.updateWheatFlourFirstGrade(oilProdTovModel);
}
return oilProdTovRepo.getOilProdTovModel();
}
}
我如何修改我的代码?
由于您似乎难以获得列表中彼此跟随的 2 个实体,因此您可以这样做:
使用“foreach”:
List<OilProdTovModel> oilProdTovModels = ...
OilProdTovModel prev = null;
for (OilProdTovModel oilProdTovModel : oilProdTovModels) {
if( prev != null ) {
//compare oilProdTovModel and prev here
}
prev = oilProdTovModel; //keep track of the current element to serve as prev for the next iteration
}
使用传统的 for 循环:
for (int i = 1; i < oilProdTovModels.size(); i++) {
OilProdTovModel prev = oilProdTovModels.get(i - 1);
OilProdTovModel current = oilProdTovModels.get(i);
//compare both - if you need to handle the first element without any comparison then do that before the loop
//(or iterate from 0 to size() - 1 and handle the last element after the loop if needed)
}
我想将当前行与上一行进行比较,如果匹配,则进行计算。如果不是,则分配 0.0 我的代码只计算而不接受条件。
例如我有table:石油 oil 包含月份、日期和指标
它必须看起来像:
Month Values Date |formula |Growth
september 100 01.09.2020 |(100/nan - 1) * 100 |infinity
september 80 08.09.2020 |(80/100-1) * 100 |-20
september 100 15.09.2020 |(100/80-1) * 100 |25
october 118 01.10.2020 |september != october |0
october 120 08.10.2020 |(120/118-1) * 100 |1.69
october 123 15.10.2020 |(123/120-1) * 100 |2.5
november 122 01.11.2020 |october != november |0
november 122 08.11.2020 |(122/122-1) * 100 |0
我的class模特:
@Entity
@Table(name = "oper_mon_oil_price")
public class OilModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "o_year")
private String oyear;
@Column(name = "o_month")
private String omonth;
@Column(name = "o_date_event")
private String odateEvent;
@Column(name = "o_period")
private String operiod;
@Column(name = "o_area")
private String oarea;
@Column(name = "o_unit")
private String ounit;
@Column(name = "o_ai_92")
public Double oai92;
@Column(name = "o_ai_95")
public Double oai95;
@Column(name = "o_diesel_winter")
private Double odieselWinter;
@Column(name = "o_diesel_fuel_offseason")
private Double odieselFuelOffseason;
@Column(name = "growth_ai_92")
public Double growthAi92;
@Column(name = "growth_ai_95")
public Double growthAi95;
@Column(name = "growth_diesel_winter")
public Double growthDieselWinter;
@Column(name = "growth_diesel_fuel_offseason")
public Double growthDieselFuelOffseason;
getters/setters
}
class 存储库:
@Repository
@Mapper
public interface OilProdTovRepo {
@Select("SELECT * FROM oper_mon_prod_tov ORDER BY o_year ASC, o_month ASC, o_date_event ASC ")
@Results(value ={
@Result(property = "id",column = "id"),
@Result(property = "area",column = "o_area"),
@Result(property = "year",column = "o_year"),
@Result(property = "month",column = "o_month"),
@Result(property = "period",column = "o_period"),
@Result(property = "dateEvent",column = "o_date_event"),
@Result(property = "unit",column = "o_unit"),
@Result(property = "wheatFlourFirstGrade",column = "o_wheat_flour_first_grade"),
@Result(property = "wheatBread",column = "o_wheat_bread"),
@Result(property = "horns",column = "o_horns"),
@Result(property = "buckwheat",column = "o_buckwheat"),
@Result(property = "polishedRice",column = "o_polished_rice"),
@Result(property = "potato",column = "o_potato"),
@Result(property = "carrots",column = "o_carrots"),
@Result(property = "onion",column = "o_onion"),
@Result(property = "cabbageWhite",column = "o_cabbage_white"),
@Result(property = "sugar",column = "o_sugar"),
@Result(property = "oil",column = "o_oil"),
@Result(property = "beef",column = "o_beef"),
@Result(property = "chickenMeat",column = "o_chicken_meat"),
@Result(property = "milk",column = "o_milk"),
@Result(property = "kefir",column = "o_kefir"),
@Result(property = "butter",column = "o_butter"),
@Result(property = "egg",column = "o_egg"),
@Result(property = "salt",column = "o_salt"),
@Result(property = "cottageCheese",column = "o_cottage_cheese"),
@Result(property = "growthWheatFlourFirstGrade",column = "o_growth_wheat_flour_first_grade"),
})
List<OilProdTovModel> getOilProdTovModel();
@Update("UPDATE oper_mon_prod_tov SET o_growth_wheat_flour_first_grade=#{growthWheatFlourFirstGrade} WHERE id=#{id}")
void updateWheatFlourFirstGrade(OilProdTovModel oilProdTovModel);
}
和主要class,服务:
@Service
public class OilProdTovService {
@Autowired
public OilProdTovRepo oilProdTovRepo;
public List<OilProdTovModel> allOilProdTov() {
List<OilProdTovModel> oilProdTovModels = oilProdTovRepo.getOilProdTovModel();
Double first = 0.0;
Double result;
for (OilProdTovModel oilProdTovModel : oilProdTovModels) {
if (oilProdTovModel.getMonth().equals("September")) {
Double wheatFlourFirstGrade = oilProdTovModel.getWheatFlourFirstGrade();
result = (wheatFlourFirstGrade / first - 1) * 100;
first = wheatFlourFirstGrade;
wheatFlourFirstGrade = result;
oilProdTovModel.setGrowthWheatFlourFirstGrade(wheatFlourFirstGrade);
} else if (oilProdTovModel.getMonth().equals("October")) {
Double wheatFlourFirstGrade = oilProdTovModel.getWheatFlourFirstGrade();
result = (wheatFlourFirstGrade / first - 1) * 100;
first = wheatFlourFirstGrade;
wheatFlourFirstGrade = result;
oilProdTovModel.setGrowthWheatFlourFirstGrade(wheatFlourFirstGrade);
}
else if (oilProdTovModel.getMonth().equals("November")) {
Double wheatFlourFirstGrade = oilProdTovModel.getWheatFlourFirstGrade();
result = (wheatFlourFirstGrade / first - 1) * 100;
first = wheatFlourFirstGrade;
wheatFlourFirstGrade = result;
oilProdTovModel.setGrowthWheatFlourFirstGrade(wheatFlourFirstGrade);
}
else {
oilProdTovModel.setGrowthWheatFlourFirstGrade(0.0);
}
oilProdTovRepo.updateWheatFlourFirstGrade(oilProdTovModel);
}
return oilProdTovRepo.getOilProdTovModel();
}
}
我如何修改我的代码?
由于您似乎难以获得列表中彼此跟随的 2 个实体,因此您可以这样做:
使用“foreach”:
List<OilProdTovModel> oilProdTovModels = ...
OilProdTovModel prev = null;
for (OilProdTovModel oilProdTovModel : oilProdTovModels) {
if( prev != null ) {
//compare oilProdTovModel and prev here
}
prev = oilProdTovModel; //keep track of the current element to serve as prev for the next iteration
}
使用传统的 for 循环:
for (int i = 1; i < oilProdTovModels.size(); i++) {
OilProdTovModel prev = oilProdTovModels.get(i - 1);
OilProdTovModel current = oilProdTovModels.get(i);
//compare both - if you need to handle the first element without any comparison then do that before the loop
//(or iterate from 0 to size() - 1 and handle the last element after the loop if needed)
}