How to optimize updating values in ArrayList <Integer>
I want to save all the values of a specific variable in a dataset and the frequency for each of these values. For this, I use ArrayList<String> to store values and ArrayList<Integer> to store frequencies (since I cannot use int ). The number of different values is unknown, so I use ArrayList , not Array .
Example (simplified):
a,b,c,d,b,d,a,c,b ArrayList<String> values with values look like this: {a,b,c,d} , and ArrayList<Integer> with frequencies looks like this: {2,3,2,2} .
To populate these ArrayLists , I repeat each entry in the dataset using the following 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<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)); } } However, since the data sets that I will use for this can be very large, I want to optimize my code, and use frequencies.set(i, new Integer((int)frequencies.get(i)+1)); It seems not very effective.
This brings me to my question; How can I optimize updating Integer values in an ArrayList ?
I would use HashMap or Hashtable as tskzzy suggested. Depending on your needs, I would also create an object that has a name, a counter, as well as other metadata that you may need.
Thus, the code will look something like this:
Hashtable<String, FrequencyStatistics> statHash = new Hashtable<String, FrequencyStatistics>(); for (String value : values) { if (statHash.get(value) == null) { FrequencyStatistics newStat = new FrequencyStatistics(value); statHash.set(value, newStat); } else { statHash.get(value).incrementCount(); } } Now your FrequencyStatistics object constructor will automatically set its count to 1, and the incrementCound () method will increment the counter and perform any other statistical calculations that may be required. It should also be more extensible in the future than storing a hash of a string with only the corresponding Integer.