字符活跃排序

给定一个字符串,统计每个Alpha字符(统一作为小写)出现的频率,并排序。

示例输入:I have A Dream…

示例输出:aedhimrv

其中a出现了3次,e出现了2次,其余字符出现一次。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static String sortMostActiveString(String in){
return in.toLowerCase().chars()
.filter(t -> t>='a' && t <='z')
.mapToObj(t -> {char[] cs = new char[1];cs[0] = (char) t;return new String(cs);})
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet().stream().sorted(
new Comparator<Entry<String, Long>>(){
@Override
public int compare(Entry<String, Long> o1, Entry<String, Long> o2) {
if (o1.getValue() > o2.getValue()) return -1;
if (o1.getValue() < o2.getValue()) return 1;
return o1.getKey().compareTo(o2.getKey());
}
}
)
.map(Entry::getKey)
.collect(Collectors.joining());
}