Java Map Operations

Adding value to Map
[code lang=”java”]
map.put(value1, value2);
}
[/code]
Method put() from Map.java 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”]
mapStringObj.putAll(mapDoubleStr);
[/code]

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();
productDiscount.setDiscount_id(discountId);
productDiscount.setDiscount_name(discountDescribe);

orderItemDiscountMap.get(orderItemKey).add(productDiscount);
}
[/code]

Leave a Reply

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