private?static?void?test()?{
String?ss?=?"Ae5a2sd35s4eEe62a35e1ao";
Pattern?p=Pattern.compile("(\\d+)");
//?使用正則表達式取出所有數字
Matcher?m=p.matcher(ss);
Map<Integer,?Integer>?cc?=?new?HashMap<Integer,?Integer>();
//?統計所有數字的出現次數。數字為key,出現次數為value存入map
while(m.find()){
int?i?=?Integer.valueOf(m.group(1));
if(cc.containsKey(i))?{
cc.put(i,?cc.get(i)?+?1);
}else?{
cc.put(i,?1);
}
??}
//?找到map中最大的value,即最大的出現次數,它對應的key則為出現最多的數字
//?可能不止一個,使用循環找出所有出現最多的數字。如果都只出現1次則全部找出
int?max?=?Collections.max(cc.values());
for(Entry<Integer,?Integer>?entry?:?cc.entrySet())?{
if(entry.getValue()?==?max)?{
System.out.println(entry.getKey()?+
"為出現最多的數,出現次數為"?+?entry.getValue()?+
".?和為"?+?entry.getKey()?*?entry.getValue());
}
}
}