java排序去重示例分享
程序员文章站
2024-02-25 11:23:40
复制代码 代码如下:package action;import java.util.arrays;import java.util.treeset;public class...
复制代码 代码如下:
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);
}
}
}