-1

Lets say I have a string "aaabcccdddd" I have stored each alphabets as keys and each repetition of the character as the value in a hash map. How can I use the map to print out to my console the characters in a descending order. For example= "dddcccaaab"

void str(String s) {
    int len = s.length();
    Map<Character, Integer> elements = new HashMap<>(Math.min(len, 26));
    Map<Character, Integer> sortedByValues;

    String concact="";        int b;char a;
    for (int i = 0; i < len; i++){
        if (!elements.containsKey(s.charAt(i)))
            elements.put(s.charAt(i), 1);
        else
            elements.put(s.charAt(i), elements.get(s.charAt(i)) + 1);}
  • 1
    show us some code to explain better what you are wanting to do. – Scary Wombat Feb 08 '17 at 00:48
  • You say you have created a hash map, I presume you did that in code? – mherzig Feb 08 '17 at 00:53
  • *string a= "abbcccddd" count a=2; count b=2, count c=3; count d=4;* **This does not make sense** – Scary Wombat Feb 08 '17 at 00:56
  • Hey I dont really have code that best describes my situation. So i hope this helps- string a= "babcdcdcd"; count a=a; count b=2, count c=3; count d=4; im storing these values in a hash map (basically how many times each alphabet appears, assuming no spaces or non-alphabets) I need to print out my highest occuring alphabet, which is d=4 and then go down the order and my final output should be a SORTED form of the initial string returned in descending order like this- "ddddcccbba" – Lazim Sharar Feb 08 '17 at 00:57
  • This is the code I have used. i just need to figure out how to sort and then print it in descending order- void str(String s) { int len = s.length(); Map elements = new HashMap<>(Math.min(len, 26)); Map sortedByValues; String concact=""; int b;char a; for (int i = 0; i < len; i++){ if (!elements.containsKey(s.charAt(i))) elements.put(s.charAt(i), 1); else elements.put(s.charAt(i), elements.get(s.charAt(i)) + 1);} – Lazim Sharar Feb 08 '17 at 00:58
  • @LazimSharar If you are sure the input is alphabetic then a simplistic approach would be to go with a primitive array to store the counts and later on process the array from the end to get your output. – boxed__l Feb 08 '17 at 00:58
  • see http://stackoverflow.com/questions/109383/sort-a-mapkey-value-by-values-java as how to sort a map by value – Scary Wombat Feb 08 '17 at 01:16

1 Answers1

1

Converting the HashMap to a TreeMap:

import java.util.*;
import java.util.stream.Collectors;

class Main {
  public static void main(String[] args) {
    String test = "aaabcccdddd";
    Map<String, Long> hashMap = 
      Arrays.stream(test.split("")).
      collect(Collectors.groupingBy(c -> c, Collectors.counting()));
    // System.out.println(hashMap); // {a=3, b=1, c=3, d=4}
    Map<String, Long> treeMap = new TreeMap(Collections.reverseOrder());
    treeMap.putAll(hashMap);
    // System.out.println(treeMap); // {d=4, c=3, b=1, a=3}
    for (Map.Entry<String, Long> entry : treeMap.entrySet()) {
      for(int i = 0; i < entry.getValue(); i++) {
        System.out.print(entry.getKey());
      }
    }
  }
}

Output:

ddddcccbaaa 

Try it here!

Sash Sinha
  • 18,743
  • 3
  • 23
  • 40