package com.shop.util;
import java.util.*;
/**
* @author Li JieGui
* @date 2020/11/12 20:25
*/
public class Main {
static class node implements Comparable<node>{
Character ch;
Integer num;
public node(Character ch, Integer num) {
this.ch = ch;
this.num = num;
}
@Override
public int compareTo(node o) {
if(o.num > this.num) {
return 1;
}else if (o.num < this.num){
return -1;
} else {
if (this.ch > o.ch) {
return 1;
} else {
return -1;
}
}
}
}
public static String getRes(String str) {
Map<Character, Integer> count = new HashMap<>();
for (int i = 0; i < str.length(); i++) {
Character key = str.charAt(i);
if(count.containsKey(key)) {
count.put(key,count.get(key)+1);
} else {
count.put(key,1);
}
}
Set<Character> characters = count.keySet();
List<node> list = new ArrayList<>();
for (Character character : characters) {
node n= new node(character,count.get(character));
list.add(n);
}
list.sort(node::compareTo);
String res = "";
for (node node : list) {
res += node.ch;
System.out.println(node.ch + " " + node.num);
}
return res;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
System.out.println(getRes(str));
}
}
Java对字符串多字段统计排序
最新推荐文章于 2022-02-15 14:48:19 发布