Intro
First of all I want to start off by saying that I am learning java so if at any point I am doing something that is inefficient or could be done better, please let me know.
What I am trying to do is sort a HashMap alphabetically by key and then return a list of the values in that order. After googling, I found that I could sort a HashMap easily using a SortedSet, but then I run into the problem of how do I get the tree set into an array?
Example
An input like this:
{"apple", "pear", "cherry", "apple", "cherry", "pear", "apple", "banana"}
Should return this:
{3,1,2,2}
My Code (so far)
import java.util.*;
public class SortedFreqs {
public int[] freqs(String[] data) {
HashMap<String, Integer> myMap = new HashMap<String, Integer>();
for (String s: data){
if (!myMap.containsKey(s)){
myMap.put(s, 0);
}
myMap.put(s, myMap.get(s)+1);
}
SortedSet<Integer> values = new TreeSet<Integer>(myMap.values());
}
}