Rk Reddy Bairi Rk Reddy Bairi - 2 months ago 9
Java Question

Duplicate Value insertion in Hashmap

There is a Hashmap (o) which takes string as a key and Order Object as value. Order has an Arraylist of OrderLines. Here I have to add multiple orders to the map. The problem is my hashmap prints out unique first and second keys (order 1 and Order 2) but the last inserted value as value for both keys (duplicate order in all entries). Can you please help me debug the problem ?

Main Class :

Map<String, Order> o = new HashMap<String, Order>();

Order c = new Order();

c.add(new OrderLine(new Item("book", (float) 12.49), 1));
c.add(new OrderLine(new Item("music CD", (float) 14.99), 1));

o.put("Order 1", c);

// Reuse cart for an other order
c.clearCart(); // this.orderLines.clear() in the order class

c.add(new OrderLine(new Item("imported box of chocolate", 10), 1));
c.add(new OrderLine(new Item("imported bottle of perfume", (float) 47.50), 1));

o.put("Order 2", c);

for (Map.Entry<String, Order> entry : o.entrySet()) {
System.out.println("*******" + entry.getKey() + entry.getValue().get(0).getItem().getDescription() + "*******");

Order class :

class Order {

private List<OrderLine> orderLines = new ArrayList<>();

public void add(OrderLine o) throws Exception {

public OrderLine get(int i) {
return orderLines.get(i);

public void clearCart() {

OrderLine class:

private int quantity;
private Item item;

public OrderLine(Item item, int quantity) throws Exception {
if (item == null) {
System.err.println("ERROR - Item is NULL");
throw new Exception("Item is NULL");
assert quantity > 0;
this.item = item;
this.quantity = quantity;

public Item getItem() {
return item;

public int getQuantity() {
return quantity;

Item class:

class Item {

private String description;
private float price;

public Item(String description, float price) {
this.description = description;
this.price = price;

public String getDescription() {
return description;

public float getPrice() {
return price;


While Java is pass-by-value, it is perfectly valid to alter what's at the end of a reference. This is what you are doing, albeit inadvertently.

Think about what you're doing: you're adding c to the list, then clearing c, re-initializing it, and adding it again.

As you never use the new keyword, you're never actually allocating a new portion of memory for c. It is still pointing to the same Order. Simultaneously, you did not add a clone of c to the list. You added c.

In other words, when you call c.clearCart(), you're also clearing the first Order in list o as said Order is c.

You could either use the new keyword by replacing:



c = new Order();

Or, you could add a clone of c to list o instead of c itself, so that when you call c.clearCart(), you are not clearing the first element in list o. In other words, replace:

o.put("Order 1", c);


o.put("Order 1", c.clone());

See this question for more information.


I forgot one part, though it may be obvious. I stated that clearing c will also clear the first element in list o, because that element is c. However, I forgot to mention that, by transition, re-initializing c also means re-initializing this element simultaneously. Consequently, when you add c again, you have two elements initialized with the same fields.