cn-chy-com
public static void test(String str){

Map<String,Integer> map = new HashMap<>();

for(int i = 0 ;i < str.length();i++){

String s = str.charAt(i) + "";
if(map.containsKey(s)){
int value = map.get(s);
map.put(s,value + 1);
}else{

map.put(s,1);

}
}

List<Map.Entry<String,Integer>> list = new ArrayList<>(map.entrySet());

Collections.sort(list,new Comparator<Map.Entry<String,Integer>>(){
@Override
public int compare(Map.Entry<String,Integer> o1,Map.Entry<String,Integer> o2){
            //从小到大排序
return o1.getValue() - o2.getValue();
            //从大到小排序
            return o2.getValue() - o1.getValue();

}

}


);

for(Map.Entry<String,Integer> m: list){

System.out.println(m.getKey() + "---" + m.getValue());

}

}

分类:

技术点:

相关文章:

  • 2018-03-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-04
  • 2021-07-20
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案