当前位置: 编程技术>java/j2ee
java排序去重示例分享
来源: 互联网 发布时间:2014-11-03
本文导语: 代码如下: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...
代码如下:
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 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);
}
}
}