I need to know how to properly iterate over an ArrayList of elements and count the number of an elements appearance in the list without before hand knowing the element. I have tried a couple of approaches and have one that currently works but I feel that it is ugly and improper.
To explain a little more in depth, I have a java web application, one of the pages will present a graph that displays the number of hits a site gets in a day/week/month. I am taking the dates that the site is hit and putting them in an array list, iterating over the array list grabbing an element and counting the number of times it appears, I then put the date as a string and its total count and putting it into a map as a key value pair of (String,int), I will then take this map and convert it to a JSON object and use it with the D3JS library to create a nice bar chart that depicts the information. Below is the code I have tried and the results it presents. The results are accurate, but rather than just working I would like to do it properly and elegant and I know I have not achieved that here. I will explain it as thoroughly as possible and look forward to feedback and ideas of a better approach.
test code main method:
public static void main(String[] args) {
Map<String, Integer> compMap = new HashMap<String, Integer>();
ArrayList<String> comp = new ArrayList<String>();
comp.add("10-31-2014");
comp.add("10-31-2014");
comp.add("10-31-2014");
comp.add("10-31-2014");
comp.add("10-15-2014");
comp.add("10-15-2014");
comp.add("10-15-2014");
comp.add("10-15-2014");
comp.add("10-15-2014");
comp.add("10-12-2014");
comp.add("10-12-2014");
comp.add("10-12-2014");
comp.add("10-12-2014");
comp.add("10-12-2014");
comp.add("10-12-2014");
comp.add("10-12-2014");
comp.add("10-12-2014");
comp.add("10-12-2014");
comp.add("10-09-2014");
comp.add("10-09-2014");
comp.add("10-09-2014");
comp.add("10-09-2014");
comp.add("10-09-2014");
comp.add("10-01-2014");
comp.add("10-01-2014");
comp.add("10-01-2014");
comp.add("10-01-2014");
comp.add("10-01-2014");
comp.add("10-01-2014");
comp.add("10-01-2014");
// 01 = 7 : 09 = 5 : 12 = 9 : 15 = 5 : 31 = 4
for (int i = 0; i < comp.size(); i++) {
int counter = 0;
String current = comp.get(i);
for (int j = 0; j < comp.size(); j++){
if (comp.get(j).equals(comp.get(i))){
counter++;
}
}
compMap.put(current, counter);
}
for (Map.Entry<String,Integer> entry : compMap.entrySet()) {
System.out.println("Key Value Pair Is: " + entry.getKey() + " : " + entry.getValue());
}
}
For testing purposes I created an array list and added multiple string dates to it (honestly I could have used an array and cut down on test code but I wanted to get as close to an actual case as possible). Afterwards I create a nested for loop, the outer for loop grabs the first item in the list and uses that as the current date for the key in the map, it is also tasked with starting the counter at 0. The inner for loop starts at zero regardless of where I is and grabs the elements of the list to compare them to element i if they are equal then counter is incremented by 1. Once the loop is completed just before exiting the outer loop the current date being worked with and the counter total is added into the map.
The results of the counter:
Key Value Pair Is: 10-15-2014 : 5
Key Value Pair Is: 10-12-2014 : 9
Key Value Pair Is: 10-31-2014 : 4
Key Value Pair Is: 10-01-2014 : 7
Key Value Pair Is: 10-09-2014 : 5
These were pulled from the console after the test ran. The results are correct. My question is more to see if there are more elegant or proper ways to perform this task?
Thank you!