Java Map Operations

Adding value to Map
[code lang=”java”]
map.put(value1, value2);
Method put() from returns
the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key, if the implementation supports null values.)

Java Converting Map to Map:
[code lang=”java”]

Java List to Map:
[code lang=”java”]
Iterable<Object[]> orderItemDiscounts = reportingService.getAllOrderItemDiscounts(businessDate, storeId);
Map<String, List<ProductDiscount>> orderItemDiscountMap = new HashMap<>();

for (Object[] objects : orderItemDiscounts) {
String orderItemKey = (String) objects[1];
String discountId = (String) objects[2];

List<ProductDiscount> orderItemDiscountList = orderItemDiscountMap.get(orderItemKey);
if (Objects.isNull(orderItemDiscountList)) {
orderItemDiscountMap.put(orderItemKey, new ArrayList<ProductDiscount>());

ProductDiscount productDiscount = new ProductDiscount();


Leave a Reply

Your email address will not be published. Required fields are marked *