java排序去重示例分享
发布时间:2016-12-28 来源:查字典编辑
摘要:复制代码代码如下:packageaction;importjava.util.Arrays;importjava.util.TreeSet;...
复制代码 代码如下:
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);
}
}
}