Как правильно добавить данные в Hash Map ?
Почему добавляется только один и тот же ключ ?
@Before
public void createCoupons() {
System.out.println("Setting coupons data! ");
Coupon coupon1 = companyServiceTesting.createCompanyCoupon(setOfCompanies.get("company1"), callForCoupon(), "Travelling low prices", "FamilyTravelling",
23, 765475475, "C://ProgramFiles", 35.8, CouponType.TRAVELING, CouponSystemDate.dateFormat(2017, 12, 12)
, CouponSystemDate.dateFormat(2019, 12, 12));
Coupon coupon2 = companyServiceTesting.createCompanyCoupon(setOfCompanies.get("company2"), callForCoupon(), "Camping low prices", "FamilyCamping",
13, 9999887, "C://ProgramFiles", 55.8, CouponType.CAMPING, CouponSystemDate.dateFormat(2016, 10, 10)
, CouponSystemDate.dateFormat(2018, 9, 1));
Coupon coupon3 = companyServiceTesting.createCompanyCoupon(setOfCompanies.get("company1"), callForCoupon(), "Electricity low prices", "Electricity",
12, 5424352, "C://ProgramFiles", 25.8, CouponType.ELECTRICITY, CouponSystemDate.dateFormat(2017, 7, 3)
, CouponSystemDate.dateFormat(2018, 4, 5));
Coupon coupon4 = companyServiceTesting.createCompanyCoupon(setOfCompanies.get("company2"), callForCoupon(), "Food low prices", "FamilyDinner",
53, 989873, "C://ProgramFiles", 15.8, CouponType.FOOD, CouponSystemDate.dateFormat(2017, 4, 11)
, CouponSystemDate.dateFormat(2018, 4, 11));
Coupon coupon5 = companyServiceTesting.createCompanyCoupon(setOfCompanies.get("company1"), callForCoupon(), "Health insurance low prices", "FamilyInsurance",
83, 33344455, "C://ProgramFiles", 88.8, CouponType.HEALTH, CouponSystemDate.dateFormat(2017, 8, 9)
, CouponSystemDate.dateFormat(2018, 8, 9));
Coupon coupon6 = companyServiceTesting.createCompanyCoupon(setOfCompanies.get("company2"), callForCoupon(), "Sport village low prices", "SportVacation",
73, 6665432, "C://ProgramFiles", 95.8, CouponType.SPORTS, CouponSystemDate.dateFormat(2017, 7, 11)
, CouponSystemDate.dateFormat(2019, 7, 11));
setInCouponCollection("coupon1", coupon1);
setInCouponCollection("coupon2", coupon2);
setInCouponCollection("coupon3", coupon3);
setInCouponCollection("coupon4", coupon4);
setInCouponCollection("coupon5", coupon5);
setInCouponCollection("coupon6", coupon6);
}
public Map<String, Coupon> setInCouponCollection(String key, Coupon coupon) {
if (setOfCoupons == null) {
setOfCoupons = new HashMap<>();
}
setOfCoupons.put(key, coupon);
System.out.println("Added object key: " + setOfCoupons.entrySet().iterator().next().getKey() + " added object value: " + setOfCoupons.get(key));
return setOfCoupons;
}
Отладка:
Setting coupons data!
Added object key: coupon1 added object value: Coupon [id=6665432, title=SportVacation, startDate=2017-07-11, endDate=2019-07-11, amount=73, message=Sport village low prices, price=95.8, image=C://ProgramFiles, type=SPORTS]
Added object key: coupon1 added object value: Coupon [id=6665432, title=SportVacation, startDate=2017-07-11, endDate=2019-07-11, amount=73, message=Sport village low prices, price=95.8, image=C://ProgramFiles, type=SPORTS]
Added object key: coupon1 added object value: Coupon [id=6665432, title=SportVacation, startDate=2017-07-11, endDate=2019-07-11, amount=73, message=Sport village low prices, price=95.8, image=C://ProgramFiles, type=SPORTS]
Added object key: coupon1 added object value: Coupon [id=6665432, title=SportVacation, startDate=2017-07-11, endDate=2019-07-11, amount=73, message=Sport village low prices, price=95.8, image=C://ProgramFiles, type=SPORTS]
Added object key: coupon1 added object value: Coupon [id=6665432, title=SportVacation, startDate=2017-07-11, endDate=2019-07-11, amount=73, message=Sport village low prices, price=95.8, image=C://ProgramFiles, type=SPORTS]
Added object key: coupon1 added object value: Coupon [id=6665432, title=SportVacation, startDate=2017-07-11, endDate=2019-07-11, amount=73, message=Sport village low prices, price=95.8, image=C://ProgramFiles, type=SPORTS]