How to optimize the updating of values in an ArrayList<Integer>
<p>I want to store all values of a certain variable in a dataset and the frequency for each of these values. To do so, I use an <code>ArrayList&lt;String&gt;</code> to store the values and an <code>ArrayList&lt;Integer&gt;</code> to store the frequencies (since I can't use <code>int</code>). The number of different values is unknown, that's why I use <code>ArrayList</code> and not <code>Array</code>.</p>

<p>Example (simplified) dataset:</p>

<pre><code>a,b,c,d,b,d,a,c,b
</code></pre>

<p>The <code>ArrayList&lt;String&gt;</code> with values looks like: <code>{a,b,c,d}</code> and the <code>ArrayList&lt;Integer&gt;</code> with frequencies looks like: <code>{2,3,2,2}</code>.</p>

<p>To fill these <code>ArrayLists</code> I iterate over each record in the dataset, using the following code.</p>

<pre><code>public void addObservation(String obs){
    if(values.size() == 0){// first value
        values.add(obs);
        frequencies.add(new Integer(1));
        return;//added
    }else{
        for(int i = 0; i&lt;values.size();i++){
            if(values.get(i).equals(obs)){
                frequencies.set(i, new Integer((int)frequencies.get(i)+1));
                return;//added
            }
        }
        // only gets here if value of obs is not found
        values.add(obs);
        frequencies.add(new Integer(1));
    }
}
</code></pre>

<p>However, since the datasets I will use this for can be very big, I want to optimize my code, and using <code>frequencies.set(i, new Integer((int)frequencies.get(i)+1));</code> does not seem very efficient.</p>

<p>That brings me to my question; how can I optimize the updating of the <code>Integer</code> values in the <code>ArrayList</code>?</p>
