java排序去重示例分享。本站提示廣大學習愛好者:(java排序去重示例分享)文章只能為提供參考,不一定能成為您想要的結果。以下是java排序去重示例分享正文
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);
}
}
}