How to compare elements and merge if condition satisfies within arraylist?
<p>i have list of trades, each trades has some attributes like source now, from the list of trades, i want to get all trades that have same source value and combine them together into one trade, for example </p>

<pre><code>tradeName      quote           source            quantity      price 
Google          GOOG           Goldman Sachs        15           610  
Microsoft       MSFT           Barclays             400          28
Google          GOOG           Goldman Sachs        45           610  
Google          GOOG           Goldman Sachs        40           610  
Microsoft       MSFT           Barclays             1000          28
</code></pre>

<p>now based on source information, i should combine trades, so my updated list of trade would be </p>

<pre><code>tradeName      quote           source            quantity        price 
Google          GOOG           Goldman Sachs        100           610  
Microsoft       MSFT           Barclays             1400           28
</code></pre>

<p>I am not sure about comparison part, how to go about solving it? </p>

<hr>

<p>Tried following approach, </p>

<pre><code>for (Trade trade : tradeList)
{
   //Not sure how to compare this.trade.source with all sources 
   //of all trades present in the trade.
   //Logic should be if source matches then quantity should be added 
   //but am not sure how comparison would work.  
}

Class Trade
{

private tradeName;
private quote;
private source;
private quantity;
private price;

//Getters and Setters for each of above mentioned attributes. 

}
</code></pre>
