I have defined an HashMap
with the following code:
final Map<OrderItemEntity, OrderItemEntity> savedOrderItems = new HashMap<OrderItemEntity, OrderItemEntity>();
final ListIterator<DiscreteOrderItemEntity> li = ((BundleOrderItemEntity) oi).getDiscreteOrderItems().listIterator();
while (li.hasNext()) {
final DiscreteOrderItemEntity doi = li.next();
final DiscreteOrderItemEntity savedDoi = (DiscreteOrderItemEntity) orderItemService.saveOrderItem(doi);
savedOrderItems.put(doi, savedDoi);
li.remove();
}
((BundleOrderItemEntity) oi).getDiscreteOrderItems().addAll(doisToAdd);
final BundleOrderItemEntity savedBoi = (BundleOrderItemEntity) orderItemService.saveOrderItem(oi);
savedOrderItems.put(oi, savedBoi);
I put 4 items into the HashMap
. When I debug, even if the size
is 4, it only shows 3 elements:
This is the list of the elements it contains.
{DiscreteOrderItemEntity@1c29ef3c=DiscreteOrderItemEntity@41949d95, DiscreteOrderItemEntity@2288b93c=DiscreteOrderItemEntity@2288b93c, BundleOrderItemEntity@1b500292=BundleOrderItemEntity@d0f29ce5, DiscreteOrderItemEntity@9203174a=DiscreteOrderItemEntity@9203174a}
What can be the problem?
Hashmaps handle collisions.
Since your HashMap
is composed by only 16 buckets, the hash of the element must be reduced to a number that spans between 0 and 15 (e.g. hash % 16
). So two elements may be in the same bucket (the same HashMapNode
).
You can inspect each HashMapNode
to find out which one contains two elements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With