How to sum up all the elements with the same property in an ArrayList in Java?
<p>I have an ArrayList&lt; Item>, and I want to do a function that sum all the quantity of the items with same code, then display all the items with the quantity.</p>

<pre><code>public class Item{
    private String code;
    private int quantity;

    public Item(String InputCode, int InputQuantity)
    {
        this.code= InputCode;
        this.quantity = InputQuantity;
    }
}
</code></pre>

<p>Suppose i do the following</p>

<pre><code>List&lt;Item&gt; items = new ArrayList&lt;Item&gt;();
items.add(new Item("A01", 1));
items.add(new Item("A02", 1));
items.add(new Item("B05", 2));
items.add(new Item("A01", 3));
items.add(new Item("Z02", 2));
items.add(new Item("A02", 2));
display();
</code></pre>

<p>then the desired output is</p>

<pre><code>A01 4
A02 3
B05 2
Z02 2
</code></pre>

<p>I have no idea, can anyone give me some hints?</p>