0

I want my code to sort my hashmap based on a value. The user will input the string and it'll count the number of occurrences. The character and its occurrence will be stored into a hasmap. It should be sorted in ascending order not caring about its keys. In my code, if they all have the same value, it sorts in alphabetical order. I don't want it to sort if they have the same values.

Desired output:
Enter a string: maabccc
m: 1
b: 1
a: 2
c: 3

Program's output:
Enter a string: maabccc
b: 1
m: 1
a: 2
c: 3

public static void main(String args[]){
    System.out.print("Enter a string: ");
    string = input.nextLine();

    char newChar[] = string.toCharArray();
    int count[] = countFrequency(string);
    Map<Character, Integer> sortedMap = sortByComparator(hm);

    for(Entry<Character, Integer> entry : sortedMap.entrySet()){
        System.out.println(entry.getKey() + " : " + entry.getValue());
    }
}
public static Map<Character, Integer> sortByComparator(Map<Character, Integer> unsortMap){
    List<Entry<Character, Integer>> list = new LinkedList<>(unsortMap.entrySet());

    Collections.sort(list, new Comparator<Entry<Character, Integer>>(){
        @Override
        public int compare(Entry<Character, Integer> o1, Entry<Character, Integer> o2){
            return o1.getValue().compareTo(o2.getValue());
        }
    });

    Map<Character, Integer> sortedMap = new LinkedHashMap<>();
    for(Entry<Character, Integer> entry : list)
        sortedMap.put(entry.getKey(), entry.getValue());

    return sortedMap;
}
Meryel
  • 33
  • 2
  • 10
  • 3
    see http://stackoverflow.com/questions/109383/sort-a-mapkey-value-by-values-java?rq=1 – Thilo May 08 '16 at 08:09
  • You seem to have already done the sorting - what is your question? – assylias May 08 '16 at 08:10
  • @assylias i want it to sort but if there's two or more that has the same value, the first inputted character should be in the first line and should not be sorted. In the code, it sorts in alphabetical order if they all have the same value. – Meryel May 08 '16 at 08:13

1 Answers1

2

I want it to sort but if there's two or more that has the same value, the first input character should be in the first line and should not be sorted. In the code, it sorts in alphabetical order if they all have the same value

Use a LinkedHashMap a hash table and linked list implementation of the Map interface, with predictable iteration order. You didn't post your countFrequency so I wrote one like,

static Map<Character, Integer> countFrequency(String str) {
    Map<Character, Integer> map = new LinkedHashMap<>();
    for (char ch : str.toCharArray()) {
        if (map.containsKey(ch)) {
            map.put(ch, 1 + map.get(ch));
        } else {
            map.put(ch, 1);
        }
    }
    return map;
}

I then changed main like

public static void main(String args[]) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a string: ");
    String string = input.nextLine();
    Map<Character, Integer> sortedMap = sortByComparator(countFrequency(string));
    for (Entry<Character, Integer> entry : sortedMap.entrySet()) {
        System.out.println(entry.getKey() + " : " + entry.getValue());
    }
}

And ran it with your input, getting your specified output.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249