M
M
Maks Burkov2017-04-06 14:12:43
Java
Maks Burkov, 2017-04-06 14:12:43

Why is it impossible to create a Hash Map with the necessary information?

How to correctly add data to Hash Map ?
Why is only the same key added?

@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;
    }

Debugging:
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]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2017-04-06
@Maks00088

Because you write incorrectly to the console.
setOfCoupons.entrySet().iterator().next().getKey()
entrySet is a set of keys, and you take an iterator - an enumerator from it, always take the first element and take its key. It is necessary to go through the set completely, for example like this:

for (Map.Entry<String, Coupon> entry: setOfCoupons.entrySet()){
    print(entry.getKey() + " ->" + entry.getValue());
}

By the way, walking around the hashmap with an iterator is a so-so occupation, because the order is not guaranteed. If you want to do this, it's better to use LinkedHashMap.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question