Possible Duplicate:
How do I iterate over each Entry in a Map?
I'm writing a program that will take an input (data), which is an array of strings, and return them in order of frequency of appearance, and then alphabetical order if they have the same number of appearances in the input. I've used a HashMap to map each string to the number of times it appears in the array, and my idea after that was to use a for loop to iterate through each number of appearances, however I'm unable to find a command that returns the number of unique values in a Hashmap. Does anyone know how to get this value?
Also, if you have a simpler way to perform the task I described, any advice is welcome.
HashMap<String, Integer> sortmap = new HashMap<String, Integer>();
ArrayList<String> stringlist = new ArrayList<String>();
ArrayList<String> stringlist2 = new ArrayList<String>();
for(String x : data)
{
if(sortmap.containsKey(x)){
sortmap.put(x, sortmap.get(x)+1);
}
else{
sortmap.put(x, 1);
}
}
for (String s : sortmap.keySet()){
for (int i : sortmap.values()){
if (sortmap.get(s) == i){
stringlist2.add(s);
}
}
}