[java]代码库
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Scanner;
public class StringChat {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
sc.close();
HashMap<Character, Integer> hashmap = new HashMap<Character, Integer>();
for (int i = 0; i < s.length(); i++) {
if (hashmap.containsKey(s.charAt(i))) {
hashmap.put(s.charAt(i), hashmap.get(s.charAt(i)) + 1);
} else {
hashmap.put(s.charAt(i), 1);
}
}
Iterator<?> iter = hashmap.entrySet().iterator();
while (iter.hasNext()) {
@SuppressWarnings("unchecked")
Entry<Character, Integer> next = (Entry<Character, Integer>)iter.next();
Entry<Character, Integer> entry = next;
Character c = (Character) entry.getKey();
Integer n = (Integer) entry.getValue();
System.out.println(c + " : " + n);
}
List<Entry<Character, Integer>> list = new ArrayList<Entry<Character, Integer>>(hashmap.entrySet());
Collections.sort(list, new Comparator<Entry<Character, Integer>>() {
@Override
public int compare(Entry<Character, Integer> o1, Entry<Character, Integer> o2) {
return o1.getValue() - o2.getValue();
}
});
StringBuilder strBuilder = new StringBuilder();
for (int i = 0; i < list.size(); i++)
for (int j = 0; j < list.get(i).getValue(); j++)
strBuilder.append(list.get(i).getKey());
System.out.println(strBuilder.toString());
}
}