代碼如下:
package action;
import java.util.Arrays;
import java.util.TreeSet;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
String strs = "ZZZ BBB AAA OOO ZZZ AAA ZZZ BBB AAA ZZZ AAA VVV OOO CCC DDD CCC CCC KKK BBB AAA ZZZ AAA CCC KKK";
String[] word = strs.split(" ");
TreeSet<String> set = new TreeSet();//去重復使用TreeSet
//排序
Arrays.sort(word);
//計算出現個數
for (int i = 0; i < word.length; i++) {
int count = 0;
for (int j = 0; j < word.length; j++) {
if (word[i].equals(word[j])) {
count += 1;
}
}
set.add(word[i]+":"+count);
}
//去重復
for(String s:set){
System.out.println(s);
}
}
}