0

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);
            }
        }
    }
Community
  • 1
  • 1
weskpga
  • 2,017
  • 7
  • 29
  • 43
  • 2
    [`TreeMap`](http://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html) is one of a handful of `Map`s that guarantee anything about its order; [`HashMap`](http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html) does not. That'd be one place to start. – Makoto Sep 26 '12 at 03:13
  • 1
    http://stackoverflow.com/questions/46898/how-do-i-iterate-over-each-entry-in-a-map – gigadot Sep 26 '12 at 03:16
  • @gigadot - That's on the right track, but is it possible to actually iterate through the list of integers alone? – weskpga Sep 26 '12 at 03:25
  • that's what your inner `for` loop does, isn't it? you don't need the outer loop if you are not doing anything with it. if you need to do anything with it, you probably need the link i sent you – gigadot Sep 26 '12 at 03:26
  • My putting it there is a bit misleading - Sorry. It does not iterate through the values unfortunately. – weskpga Sep 26 '12 at 03:33

2 Answers2

1

Double looping at the end is very unfortunate.

Take sortmap.entrySet() and store it in an array. Then sort that array with Arrays.sort using your own Comparator which first takes into account the counters and if they are equal, compares strings alphabetically.

Alexei Kaigorodov
  • 13,189
  • 1
  • 21
  • 38
0

I figured it out - here's the full logic for people who were wondering:

public String[] sort(String[] data) {
    TreeMap<String, Integer> sortmap = new TreeMap<String, Integer>();
    ArrayList<String> stringlist = new ArrayList<String>();
    for(String x : data){
        if(sortmap.containsKey(x))
            sortmap.put(x, sortmap.get(x)+1);
        else
            sortmap.put(x, 1);
    }
    Arrays.sort(sortmap.values().toArray(), 0, sortmap.size());
    for (int i = data.length; i > 0; i--){
        for (Entry<String, Integer> k : sortmap.entrySet()){
            if (k.getValue() == i)
                stringlist.add(k.getKey());
        }
    }
    String[] output = stringlist.toArray(new String[stringlist.size()]);
    return output;
}
weskpga
  • 2,017
  • 7
  • 29
  • 43